动作条上导航再现父活动来代替的onResume(ActionBar up navigation re

2019-07-22 12:15发布

我使用了推荐的方式向上导航和我的代码如下所示:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent h = new Intent(ShowDetailsActivity.this, MainActivity.class);
            h.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(h);
            return true;
        default: return super.onOptionsItemSelected(item);
    }
}

这里的用例:

  1. 我启动我的应用程序,这是“MainActivity”
  2. 我点击一个按钮,进入“ShowDetailsActivity”
  3. 我点击向上动作条导航

问题是当我点击UP,MainActivity它的onCreate()方法一遍击中,失去而不是在典型的onResume()开始,像它会所有的状态,如果我只是被称为由ShowDetailsActivity“完成()”。 为什么? 这是它是如何工作的始终,这有望为Android的行为来重建被导航到使用“向上”导航方式的所有活动? 如果我打后退按钮我得到预期的onResume生命周期。

这是我的解决方案,如果不存在一个Android“正确”的人:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent upIntent = new Intent(this, MainActivity.class);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                NavUtils.navigateUpTo(this, upIntent);
                finish();
            } else {
                finish();
            }
            return true;
        default: return super.onOptionsItemSelected(item);
    }
}

Answer 1:

以下添加到您的父活动清单文件

android:launchMode="singleTop"

关于这个答案



Answer 2:

究其原因 ,该活动在使用了导航是,Android使用标准启动模式这一点,如果没有指定其他模式重建。 这意味着

“该系统始终处于目标任务创建活动的新实例”

因而活性重建(见实况这里 )。

一种解决方案是要么宣布在MainActivity的启动模式

android:launchMode="singleTop"

AndroidManifest.xml
(应始终共同努力Intent.FLAG_ACTIVITY_CLEAR_TOP

或者你可以添加FLAG_ACTIVITY_SINGLE_TOP到你的意图的标志,告诉活动,它不应该被重建,如果是回栈,例如在

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


Answer 3:

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.



Answer 4:

在元件parentActivityName属性:Android中4.1(API级16)开始,可以通过指定机器人声明每个活动的逻辑父。 如果您的应用支持Android 4.0及更低,包括支持库与您的应用程序,并添加里面的一个元素。 然后指定为android.support.PARENT_ACTIVITY值父活动,匹配了android:parentActivityName属性。

<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>

如果父活动具有发射模式,或向上意图包含FLAG_ACTIVITY_CLEAR_TOP,父活动被带到堆栈的顶部,并通过其onNewIntent()方法接收的意图。

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

    </activity>

在上面的代码中“SingleTop”,一个“singleTop”活动的新实例也可以创建来处理新的意图。 然而,如果目标任务已在其堆栈顶部的活动的现有实例,该实例将接收新的意图(在onNewIntent()调用); 不创建一个新的实例。

查看详细文档点击这里

要浏览了当用户按下应用程序图标,你可以使用NavUtils类的静态方法,navigateUpFromSameTask()。 对于阅读上面的链接中给出的文件建立。



Answer 5:

什么工作对我来说(我也觉得最干净)是将活动重新排序到前面。 如果它不能在栈上存在为你的资产净值达',它会被创建。

Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(upIntent);
finish();
return true;


Answer 6:

你只需要回去,不要再创建活动。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            super.onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}


文章来源: ActionBar up navigation recreates parent activity instead of onResume