Pages Navigation Menu

Coding is much easier than you think

LEARN TO USE FRAGMENTS IN YOUR ACTIVITY

LEARN TO USE FRAGMENTS IN YOUR ACTIVITY

In this Tutorial, you will Learn about how to use Fragments in your Activity

Here you go!!!!

 

 

 

 

Fragment:

A fragment represents a particular operation or interface running within a larger activity. Most fragments define their own layout of views that live within the activity’s view hierarchy. Or, you can create, add, and remove fragments dynamically in an activity at run-time. Fragments are components which run in the context of an Activity. Fragment components encapsulate application code so that it is easier to reuse it and to support different sized devices.

You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. When you add a fragment as a part of your activity layout, it lives in a ViewGroup inside the activity’s view hierarchy and the fragment defines its own view layout. You can insert a fragment into your activity layout by declaring the fragment in the activity’s layout file, as a <fragment> element, or from your application code by adding it to an existing ViewGroup.

Need for Fragments:

Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet’s screen is much larger than that of a handset, there’s more room to combine and interchange UI components. Fragments allow such designs without the need for you to manage complex changes to the view hierarchy. By dividing the layout of an activity into fragments, you become able to modify the activity’s appearance at runtime and preserve those changes in a back stack that’s managed by the activity.

Classes involved for Fragments :

1. The primary classes related to fragments are:

android.app.Fragment

2. The base class for all fragment definitions

android.app.FragmentManager

3. The class for interacting with fragment objects inside an activity

android.app.FragmentTransaction

The class for performing an atomic set of fragment operations

Fragment Lifecycle:

The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment. For example, when the activity receives onPause(), each fragment in the activity receives onPause().

Basic methods:

onCreate()

onCreate(Bundle)

The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

onCreateView()

onCreateView(LayoutInflater, ViewGroup, Bundle)

The system calls this when it’s time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment’s layout. You can return null if the fragment does not provide a UI.

onPause()

The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).

Use of Inflator:

inflater.inflate(R.layout.example_simplecodestuffs, container, false);

The inflate() method takes three arguments:

  • The resource ID of the layout you want to inflate.
  • The ViewGroup to be the parent of the inflated layout. Passing the container is important in order for the system to apply layout parameters to the root view of the inflated layout, specified by the parent view in which it’s going.
  • A boolean indicating whether the inflated layout should be attached to the ViewGroup (the second parameter) during inflation. (In this case, this is false because the system is already inserting the inflated layout into the container—passing true would create a redundant view group in the final layout.)

Types of Fragment:

Static:

The easiest way to incorporate a fragment into an activity is by including it directly into the activity’s layout file. In the activity’s layout file, simply use the <fragment> element to achieve this. However, this approach does not allow you to dynamically remove the fragment at run-time.

Dynamic:

A great feature about using fragments in your activity is the ability to add, remove, replace, and perform other actions with them, in response to user interaction. Each set of changes that you commit to the activity is called a transaction and you can perform one using APIs in FragmentTransaction.

To manage the fragments in your activity, you need to use FragmentManager. To get it, call getFragmentManager() from your activity.

The below code will help you to use fragment manager. Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.

FragmentManager fragmentManager = getFragmentManager()

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. For example:

simplecodestuffsfrag fragment = new simplecodestuffsfrag();

fragmentTransaction.add(R.id.fragment_container, fragment);

fragmentTransaction.commit();

 

 

Click here to Share It..!!

Share

  • Virat Bhavsar

    class abc extends Fragment{

    oncreateView();

    View v = infleter.inflte(R.layout.xyz , null);

    return v;
    }

  • Aslam

    excellent post

    • Jamil

      :)

  • Jerome

    quite nice post, i certainly enjoy this web site, keep on it