Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Restoring order of fragments in the stack

I got 2 activities, A and B. Each activity is a container for fragments which are replaced with a FragmentTransaction.

I got an issue on some devices that when a user opens Activity B while he was in Activity A, the first activity is probably destroyed, which means that when a user clicks the back button, it makes the first activity recreated while in a normal device, it would just resume.

My main issue is that the user loses its fragment stack he had in the first activity. When the user opened the 2nd activity, he was already 3 fragments "deep" the first activity. How can I restore the stack and return the user to the point he's been before the first activity was destroyed?

like image 796
Yonatan Nir Avatar asked Nov 07 '22 17:11

Yonatan Nir


1 Answers

This should be handled by the Android OS automatically. You can turn developer option "don't keep activities" on to always mimic this behavior (destroying your activity) when your activity goes to the background. After that you can start debugging. Some things to check:

  • In onCreate of the activity, are you calling the super onCreate with the savedInstanceState?

  • If you put a breakpoint at the start of onCreate, when you "come back" to the activity, is there a saved instance state?

  • Where are you creating the fragments? Are you re-creating them manually (you shouldn't)?

  • Are your fragments hardcoded in the layout or replaced in the layout (replacing a container view)?

* EDIT *

From your reply I derive that this is the problem, you say: "In the end of the onCreate I am replacing the fragment with a fragment transaction and thus load the first fragment of the app" => you should not do that when the savedInstanceState is not null. Otherwise you're destroying what is already there from the saved state.

Check here: https://developer.android.com/training/basics/fragments/fragment-ui.html

Notice the return when savedInstanceState is not null.

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

        // Check that the activity is using the layout version with
        // the fragment_container FrameLayout
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create a new Fragment to be placed in the activity layout
            HeadlinesFragment firstFragment = new HeadlinesFragment();

            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }
    }
}
like image 196
Frank Avatar answered Nov 14 '22 21:11

Frank