The following two tabs change content below.
Hi, I have written and developed this site to share my experience and ideas with other colleagues. I also started to prepare interview questions and answers for job seekers. I hope it will help you a lot.

What is a fragment?

A Fragment represents a behaviour 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 also will be in paused state, and when the activity is destroyed, so are all fragments will be destroyed.

Manage Fragment Transaction:

To change fragment like add/remove/replace in that case you must use APIs from Fragment Transaction. You can get an instance of Fragment Transaction from your Activity like this:

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

Suppose you want to add a fragment into your activity’s view then you have to follow below code:

ExampleFragment fragment = new ExampleFragment();

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

fragmentTransaction.commit();

 

In the above code snippet add() method have two parameter

  1. Layout id of the activity’s view where you want to add fragment
  2. Fragment object which you want to add into activity layout

For replace a fragment:

fragmentTransaction.replace(R.id.fragment_container, newFragment);

Note: 

After all changes make sure you are calling commit() function on fragment transaction without this your changes will not be visible.

// Create new fragment and transaction

Fragment newFragment = new ExampleFragment();

FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,

// and add the transaction to the back stack

transaction.replace(R.id.fragment_container, newFragment);

transaction.addToBackStack(null);

// Commit the transaction

 

transaction.commit();

 

Manage Fragment Stack:

In the above example we mention addToBackStack(String name) means this fragment will add into stack . Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

And Parameter Name: An optional name for this back stack state, or null.

 

To traverse accurately all fragments you can override onBackPressed() function on your main activity also.

Like:

@Override

public void onBackPressed() {

    FragmentManager fm = getFragmentManager();

    if (fm.getBackStackEntryCount() > 0) {

        fm.popBackStack();

    } else {

        super.onBackPressed();

    }

}

39,334 total views, 23 views today

16 thoughts on “Understanding of fragments and manage fragment stack and transaction in android”

  1. Simple and easy way to understand the concept of fragments for beginners Good Explanation with example to clear basics of fragments.

  2. I have been browsing on-line more than three hours lately,
    yet I never discovered any fascinating article like yours.
    It’s beautiful price enough for me. In my opinion, if all webmasters
    and bloggers made excellent content material as you probably did, the web
    will likely be a lot more useful than ever before. http://www.yahoo.net/

  3. We are a group of volunteers and opening a new scheme in our community.
    Your website offered us with valuable information to work on. You’ve done a formidable process and our entire community might be grateful to you.

  4. I do accept as true with alll the ideas you’ve introduced in your post.

    They are really convincing and will definitely work. Nonetheless, the
    posts are too short for starters. May just you please
    prolong them a little from subsequent time?
    Thank you for the post.

  5. Pretty nice post. I just stuumbled upon your blog and
    wished to say that I’ve truly enjoyed surfing around your blog posts.
    In any case I will be subscribing to your feed and
    I hope you writre again very soon!

  6. Good day I am so glad I found your blog, I really found you by
    mistake, while I was searching on Aol for something else, Nonetheless I
    am here now and would just like to say thanks for a marvelous post and
    a all round thrilling blog (I also love the theme/design), I don’t
    have time to read it all at the moment but I have bookmarked it and
    also included your RSS feeds, so when I have time I will be back to read a
    great deal more, Please do keep up the superb work.

Leave a Reply

Your email address will not be published. Required fields are marked *