ActionBar up navigation recreates parent activity instead of onResume

前端 未结 6 2023
天涯浪人
天涯浪人 2020-12-04 16:26

I\'m using the recommended approach for Up Navigation and my code looks like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switc         


        
6条回答
  •  情深已故
    2020-12-04 17:04

    The reason, the activities are recreated when using up-navigation is, that android uses standard launch mode for this, if you do not specify an other mode. That means

    "The system always creates a new instance of the activity in the target task"

    and thus the activity is recreated (see docu here).

    A solution would be to either declare the launch mode of the MainActivity as

    android:launchMode="singleTop"
    

    in the AndroidManifest.xml
    (should always work together with Intent.FLAG_ACTIVITY_CLEAR_TOP)

    or you could add FLAG_ACTIVITY_SINGLE_TOP to your intent flags, to tell the activity, that it should not be recreated, if it is on the back stack, e.g.

    Intent h = NavUtils.getParentActivityIntent(this); 
    h.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    NavUtils.navigateUpTo(this, h);
    

提交回复
热议问题