I have a main activity that calls fragments depending on a condition, so I did the following in onResume of MainActivity:
Fragment f = getSupportFragmentManager().findFragmentById(R.id.container);
if (f != null) {
d.log("frag active, goes to current frag");
} else if (userId != 0 && f == null) {
d.log("active user frag is null, go to fragment X");
getSupportFragmentManager().beginTransaction().replace(R.id.container, new FragmentX()).commit();
} else {
d.log("no user, no frag, go to login");
getSupportFragmentManager().beginTransaction().replace(R.id.container, new LoginPage()).commit();
}
I added
setRetainInstance(true);
in all fragment's onCreate. Now for the problem: If I press the recent apps button (multi window where I can select an app) go to a different app and then do the same and go back to my app, everything is OK, the first condition is met as expected.
But... if I press the home button and there I press the icon of my app, it opens it but condition 3 is met so it goes to the login page, instead of the last active fragment.
What am I doing wrong?