Sunday 4 October 2015

Fragments

Fragments
  • A Fragment represents a behavior or a portion of user interface in an Activity.
  • You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities.
  • A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle.
  • For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments.
  • 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.

    Example For Fragment



    Fragment Lifecycle


    onAttach():
  • Called when a fragment is first attached to its context.
onCreate():
OnCreateView():
  • 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.
    onStart ():
  • Called when the Fragment is visible to the user. This is generally tied to Activity.onStart of the containing Activity's lifecycle.
onResume():
  • The fragment is visible in the running activity.
onPause():
  • Another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible (the foreground activity is partially transparent or doesn't cover the entire screen).
onStop():
  • The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed.

    Adding a fragment to an activity
    Usually, a fragment contributes a portion of UI to the host activity, which is embedded as a part of the activity's overall view hierarchy. There are two ways you can add a fragment to the activity layout:

    1. Static Fragment
    2. Dynamic Fragment
      Static Fragment:
      • The android:name attribute in the <fragment> specifies the Fragment class to instantiate in the layout.
      • When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each one, to retrieve each fragment's layout.
      • The system inserts the View returned by the fragment directly in place of the <fragment> element.

      Dynamic Fragment:
      1. At any time while your activity is running, you can add fragments to your activity layout. You simply need to specify a ViewGroup in which to place the fragment.
      2. To make fragment transactions in your activity (such as add, remove, or replace a fragment), you must use APIs from FragmentTransaction.
      3. You can get an instance of FragmentTransaction from your Activity like this:
                       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:

                               ExampleFragment fragment = new ExampleFragment();
                               fragmentTransaction.add(R.id.fragment_container, fragment);
                               fragmentTransaction.commit();

      4.     Once you've made your changes with FragmentTransaction, you must call commit() for the changes to take effect.

       Adding a fragment:
      Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) inserted into a container view of the activity.
      public abstract FragmentTransaction add (int containerViewId, Fragment fragment)
      Parameters:
      containerViewId     Optional identifier of the container this fragment is   to be placed in. If 0, it will not be placed   
                                      in a container.
      fragment                 The fragment to be added. This fragment must not already be added to the activity.
      Returns :
      Returns the same FragmentTransaction instance.

       Committing the fragment transaction:

      1. Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.
      2. A transaction can only be committed with this method prior to its containing activity saving its state.
      3. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss() for situations where it may be okay to lose the commit.
      4. public abstract int commit ()
      Returns:
      Returns the identifier of this transaction's back stack entry, if addToBackStack(String) had been called. Otherwise, returns a negative number.

       
     
     

0 comments:

Post a Comment