I have two activities:
- MainActivity, which extends FragmentActivity and has
- 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?
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)