1

I'm having some problems with navigation and the up button of the activity. I have two tabs implemented with Viewpager in one activity and then another activity which is loaded from the previous one. When the user clicks the phone's back button or the activity's up button, I want to go back to the previously selected tab and fragment of the first activity.

So far I've been able to do it for the back button, with onSaveInstanceState and the following code:

@Override
protected void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
    bundle.putInt("currentPage", mViewPager.getCurrentItem());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState != null) {
        int page = savedInstanceState.getInt("currentPage", 0);
        mViewPager.setCurrentItem(page);
        getSupportActionBar().setSelectedNavigationItem(page);
    }
}

Why is this not working for the up button? What do I have to do?

Any help is appreciated and sorry if this is an issue easy to solve, but I'm rather new to the Android programming.

fischerito
  • 21
  • 5

1 Answers1

1

I finally found the solution at Returning from an activity using navigateUpFromSameTask(). savedInstanceState was null when the activity was recreated. To avoid so, the launch mode of the activity has to be declared as singleTop in the Android manifest.

Community
  • 1
  • 1
fischerito
  • 21
  • 5