How to go to the previous selected tab when you click the back button of the action bar

I have six tabs in MainActivity, the second tab has a listview, when the user clicks on the listview element, opens a new Activity with Action panel, so when the user clicks the back button of the second action, I want to go to the previous tab (second tab) of the main operation , but its loading of the first tab (tab "Home").

How can I solve this problem?

+6
source share
2 answers

Here we have three cases. The actual return button (regardless of hardware or software), the parent element of the Action Bar ("Up"), and both buttons:

  • Back Button Button :

When you call SecondActivity , use startActivityForResult() to save the MainActivity information about the SecondActivity life cycle. When the back MainActivity.onActivityResult() pressed, write this event to MainActivity.onActivityResult() and switch to the second tab:

MainActivity: where you start your activity now :

 // Start SecondActivity that way. REQUEST_CODE_SECONDACTIVITY is a code you define to identify your request startActivityForResult(new Intent(this, SecondActivity.class), REQUEST_CODE_SECONDACTIVITY); 

MainActivity: onActivityResult () :

 // And this is how you handle its result @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode == REQUEST_CODE_SECONDACTIVITY && resultCode == RESULT_CANCEL) { switchToTab(2); // switch to tab2 } // else... other cases } 
  • Upper Button Action Panel :

If this behavior should be connected to the Control Panel button instead of the Back button, you must override getSupportParentActivityIntent() or getParentActivityIntent() depending on whether you use the support library or not.

SecondActivity: get [Support] ParentActivityIntent () :

 @Override public Intent getSupportParentActivityIntent() { // getParentActivityIntent() if you are not using the Support Library final Bundle bundle = new Bundle(); final Intent intent = new Intent(this, MainActivity.class); bundle.putString(SWITCH_TAB, TAB_SECOND); // Both constants are defined in your code intent.putExtras(bundle); return intent; } 

And then you can handle it in MainActivity.onCreate() .

MainActivity: onCreate () :

 @Override protected void onCreate(Bundle savedInstanceState) { ... final Intent intent = getIntent(); if (intent.hasExtra(SWITCH_TAB)) { final int tab = intent.getExtras().getInt(SWITCH_TAB); switchToTab(tab); // switch to tab2 in this example } ... 
  • Both cases of buttons:

If you want to handle both buttons the same way (whether it’s a good idea or not, I just don’t know), both of the above solutions can be implemented simultaneously without any problems.

Side note: To determine if this is a good idea or not, this official guide can help. Especially the section "Navigation using the application icon."

+7
source

Using the Up navigation to return to parent activity re-creates parent activity. When parent activity is recreated, you lose the selected tab. Instead of saving the selected tab, it’s easier not to recreate the parent activity. You can do this by adding the following line to the parent activity section of your AndroidManifest file.

 android:launchMode="singleTop" 

This will prevent the parent from being recreated, and thus your previously selected tab will be in the same state.

For example, if MainActivity is a parent with tabs, then the manifest will look something like this:

 <activity android:name=".MainActivity" android:launchMode="singleTop"> ... </activity> 

See also:

+7
source

Source: https://habr.com/ru/post/973082/


All Articles