Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment PopBackStack

I am getting a strange problem, while using Fragments and popping back them out.

I have one Fragment Activity:

Step1: I am attaching one Fragment in the onCreate of that Activity in the Starting named Fragment A as:

This is the onCreate of Fragment Activity

@Override
    protected void onCreate(Bundle savedBundleState) {
        super.onCreate(savedBundleState);
        setContentView(R.layout.activity_base_new);

        Fragement_Home home = new Fragement_Home();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().add(R.id.frameContent, home).addToBackStack("home").commit();
    }

Step:2 After that I attached replaced the Fragment with the following code:

Fragment_More moreFragment = new Fragment_More();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.frameContent, moreFragment).addToBackStack("more").commit();

Step 3: After that again I added one more Fragment on Fragment_More in the same way:

Fragment_AboutRockstand termsConditions = new Fragment_AboutRockstand();
            getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frameContent, termsConditions).addToBackStack("about_rockstand").commit();

and here is my Fragment's Activity onBackPressedButton Lisetener:

@Override
public void onBackPressed() {
    FragmentManager fm = getSupportFragmentManager();
    Fragment f = fm.findFragmentById(R.id.frameContent);

 if(fm.getBackStackEntryCount() > 1){
         fm.popBackStack();
    } else{
        finish();
    }
}

Here is the Problem which I am getting:

When I press back from Step 3, it gives me previous fragment successfully as described by onBackPressed, it will popbackstack and will take me to the previous Fragment. But from the Fragment_More, when I press back again, it shows me blank FRAGMENT content. It never shows the First Fragment which I had added on Step 1 in the OnCreate of the Activity.

Please comment. What I am doing wrong ?

Thanks

like image 537
Gaurav Arora Avatar asked Sep 19 '14 11:09

Gaurav Arora


People also ask

Does popBackStack destroy fragment?

FragmentManager popBackStack doesn't remove fragment.

What is the use of popBackStack in Android?

popBackStack. Pop all back stack states up to the one with the given identifier. This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

What is popBackStack inclusive?

popBackStack(backStackId, INCLUSIVE) will pop all the fragments (states) back to backStackId, so once you pop the lowest i that you're going to pop, all the higher ones should get popped at the same time.


3 Answers

You should not add your first fragment to the backstack, as pressing the return key will just reverse the action you did. So if you replace a fragment and you press back, it will reverse the replace you did. But if you press back on an '.add()' fragmenttransaction, it will simply remove it and show you a blank page. So i think removing the first fragment from the backstack should do the trick. Try it out and let me know!

@Override     protected void onCreate(Bundle savedBundleState) {         super.onCreate(savedBundleState);         setContentView(R.layout.activity_base_new);          Fragement_Home home = new Fragement_Home();         FragmentManager manager = getSupportFragmentManager();         manager.beginTransaction().add(R.id.frameContent, home).commit();     } 

EXAMPLE:

So let's say you have two fragments called A & B

If you .add() fragment A to the page, and "addToBackStack()" ( = save reverse of the action ) you will have saved the REMOVE fragment A as action when you click on back.

If you .replace() fragment A by fragment B, and also "addToBackStack()" you will have saved the remove B and add A as action when you click on back.

So you see why it's not good practice to add your first fragmenttransaction to the backstack? It will just result in removing fragment A by pressing back, resulting in a blank page. Hope this is enough info.

Also, once you have removed the addToBackStack() to your first fragment, you don't need to override onBackPressed() because it will work how it is supposed to. It will just finish the activity when there is nothing left in the backstack without showing you that blank page first.

like image 154
Chris Avatar answered Sep 20 '22 06:09

Chris


Your line:

if(fm.getBackStackEntryCount() > 1){ 

should be:

if (fm.getBackStackEntryCount() > 0) { 

You are putting 3 fragments on the backstack. After you pop the first two entries off, the count of entries on the backstack then equals 1. So you prevent yourself from retrieving the first fragment you added to the backstack.

like image 45
Jerry Frost Avatar answered Sep 19 '22 06:09

Jerry Frost


You are using this

manager.beginTransaction().add(R.id.frameContent, home).addToBackStack("home").commit();

try to use this instead

manager.beginTransaction().replace(R.id.frameContent, home).addToBackStack("home").commit();

Let me know if it works for you.

like image 20
mujeeb.omr Avatar answered Sep 18 '22 06:09

mujeeb.omr