0

I have two activities:

  1. MainActivity, which extends FragmentActivity and has
  2. ViewActivity, which is just an Activity, having MainActivity as a parent.

There is a navigation from one of the Fragments of MainActivity to the ViewActivity. The problem is that when I'm navigating back from ViewActivity (pressing Back button) to the MainActivity I'm getting the first Fragment of the activity, loosing all the backstack of fragments, that I had.

Is there a way to get back to FragmentActivity without loosing Fragments backstack? How can I get back to the Fragment, which started ViewActivity?

Some kind of visualization

UPDATE

Here is the code for my current onCreate from MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Listen for changes in the back stack
    getSupportFragmentManager().addOnBackStackChangedListener(this);
    //Handle when activity is recreated like on orientation Change
    shouldDisplayHomeUp();
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        MainFragment mainFragment = new MainFragment();
        getSupportFragmentManager().beginTransaction()
            .add(R.id.a_main_container, mainFragment)
            .commit();
    }
}

UPDATE 2 Finally, found out that the problem was, when I was clicking Up button in ActionBar. Now I removed the actionbar from ViewActivity (just do not need it there), and everything works fine. Usual Back button moves me to Fragment 3, then to Fragment 2, etc.

But is it possible to make the same with UpButton in ActionBar? (Asking just for future usage)

Demian
  • 115
  • 1
  • 10

2 Answers2

1

Seems you instantiate your Fragment 1 in onCreate of your MainActivity without checking for savedInstanceState == null. So each time you navigate back to your MainActivity - it istantiates Fragment 1 from scratch. Post more code so i will point you to right direction.

Answering UPDATE 2:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
localhost
  • 5,568
  • 1
  • 33
  • 53
0

as i see it you don't know which fragment to go back to. what happens when you press back button again?

also would be helpful to see the code of your onCreate() activity

As I see it what you need to do is to use saveInstancestate to remember the fragment you were at.

I think this question would be helpful Once for all, how to correctly save instance state of Fragments in back stack?

Community
  • 1
  • 1
itai
  • 294
  • 1
  • 14
  • `saveInstanceState()` is to save the current state of an object, the OP is asking how to navigate back in reverse order in which the fragment transactions were commited. – Biu Jan 17 '15 at 21:05
  • you save the state of the activity so when you go back and it rebuilt it will know to which fragment to go back to or i was miss understanding the question – itai Jan 17 '15 at 21:14
  • @user3364227, yes, now I understand that I am able to save a fragment, but how can I restore the whole backstack to be able to navitage with back botton throught the following route: ViewActivity->Fragment 3->Fragment 2? – Demian Jan 17 '15 at 21:17
  • do you kill your activity? else it should be saved unless you empty it ofc – itai Jan 17 '15 at 21:19
  • @itai, I'm not killing it. Just calling startActivity(intent); from one of its fragments. – Demian Jan 17 '15 at 21:32
  • ok so what happens when you press back after you were sent to the first fragment? is it close the app or go back to fragment number 3 also after you save instancestate make sure you didn't reintalize fragment manager – itai Jan 17 '15 at 22:06