ActionBar up navigation recreates parent activity instead of onResume

前端 未结 6 2022
天涯浪人
天涯浪人 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 16:38

    What works for me (and I also think the cleanest) is to reorder the activity to front. If it doesn't exist on the stack it will be created as you 'nav up'.

    Intent upIntent = NavUtils.getParentActivityIntent(this);
    upIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(upIntent);
    finish();
    return true;
    
    0 讨论(0)
  • 2020-12-04 16:45

    Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the android:parentActivityName attribute in the element. If your app supports Android 4.0 and lower, include the Support Library with your app and add a element inside the . Then specify the parent activity as the value for android.support.PARENT_ACTIVITY, matching the android:parentActivityName attribute.

    <application ... >
    ...
    <!-- The main/home activity (it has no parent activity) -->
    <activity
        android:name="com.example.myfirstapp.MainActivity" ...>
        ...
    </activity>
    <!-- A child of the main activity -->
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
    

    If the parent activity has launch mode , or the up intent contains FLAG_ACTIVITY_CLEAR_TOP, the parent activity is brought to the top of the stack, and receives the intent through its onNewIntent() method.

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

    In Above Code "SingleTop" ,a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created.

    For Detail Documentation Click Here

    To navigate up when the user presses the app icon, you can use the NavUtils class's static method, navigateUpFromSameTask(). For That Read The Documentaion given in the Link Above.

    0 讨论(0)
  • 2020-12-04 16:50

    you just need to go back , not to create the activity again.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                super.onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
  • 2020-12-04 16:52

    This code worked for me:

    Intent upIntent = NavUtils.getParentActivityIntent(this);
    if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
        // This activity is NOT part of this app's task, so create a new task
        // when navigating up, with a synthesized back stack.
        TaskStackBuilder.create(this)
            // Add all of this activity's parents to the back stack
            .addNextIntentWithParentStack(upIntent)
            // Navigate up to the closest parent
            .startActivities();
    } else {
        // This activity is part of this app's task, so simply
        // navigate up to the logical parent activity.
        upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        NavUtils.navigateUpTo(this, upIntent);
    }
    
    return true;
    

    No finish() invocation required.

    0 讨论(0)
  • 2020-12-04 17:02

    Add the following to your parent activity in the manifest file

    android:launchMode="singleTop"
    

    regarding to this answer

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题