0

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?

Amos
  • 1,321
  • 2
  • 23
  • 44
  • might be of interest, http://stackoverflow.com/questions/4341600/how-to-prevent-multiple-instances-of-an-activity-when-it-is-launched-with-differ – petey Jan 02 '15 at 15:41
  • Thanks but it doesn't seem to be related to my problem. MainActivity is always launched but the HOME button kills it and its active fragment and I'm sure I'm missing something here. – Amos Jan 02 '15 at 15:49

0 Answers0