简体   繁体   中英

Restore activity with right fragment

I have a main activity that holds fragments. If I press home button and then re-enter my app, I want to restore the fragments backstack as I had never left. Is this possible? I may i do it?

I don't understand pretty well your question but if you want to come back to the previous fragment, you have to use addToBackStack(null).

Imagine that you are in one fragment: FragmentA and you call to the Fragment B

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    ft.replace(R.id.fragments_container, fragment);
    ft.setTransition(FragmentTransaction.TRANSIT_NONE);

        ft.addToBackStack(null);

    ft.commit();

If you don't add the line ft.addToBackStack(null), when the user press the back button from Fragment B, you never are going to come back to Fragment A. But if you use this line, mean that you are going to include the Fragment A in the queue of fragments before to pass to Fragment B. In that way, when the back button is pressed, you will come back to Fragment A.

Was that your question?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM