向上导航不启动父级活动 [英] Up Navigation not launching parent activity

查看:96
本文介绍了向上导航不启动父级活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动AB,其中A是B的父级.现在,我显示一个启动B的通知.当我点击通知时,B启动.然后,点击按钮.当活动A在后退堆栈中时,它可以正常工作,否则应用程序将关闭并且不启动活动A.

I have two activities A and B where A is the parent of B. Now I show a notification that launches B. when I tap on the notification, B launches. Then I click on the up button. It works finr when activity A is in the backstack but otherwise the app Just closes and does not launch activity A.

我在A中的 SingleTop launchMode

I have declared A as Parent of B in Manifest with A in SingleTop launchMode

<activity
        android:name=".A"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="<packegeName>.Home" />
        </intent-filter>
    </activity>
    <activity
        android:name=".B"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:parentActivityName=".A"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".A" />
    </activity>

这是通知意图:

Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
                                                        PendingIntent.FLAG_ONE_SHOT);

在活动B中:

@Override
public void onCreate(Bundle savedInstanceState) {
    ....
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ....
}

我尝试过的

为了进行调试,我尝试在活动B的onResume中手动触发所有这些导航:

What I've tried

In order to debug I've tried manually triggering this up navigation in Activity B's onResume, with all of these :

Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
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.
    NavUtils.navigateUpTo(this, upIntent);
}

2)

onNavigateUp();

3)

NavUtils.navigateUpFromSameTask(this);

但是,当Activity A不在Backstack中时,所有这些似乎都不起作用,该应用程序将退出.

But none of these seem to work when Activity A is not in the Backstack, the app just exits.

我已经搜寻了SO和Google(甚至是bing!)来寻找答案,并尝试了所有我无法找到的尝试.由于没有错误或日志输出,所以我也不知道如何调试它.

I have scoured SO and Google (even bing!) looking for an answer and tried everything I could find to no avail. As there is no error or log output, I also have no idea how to debug this.

任何解决问题的方法或提示将不胜感激.

Any solution or tips on finding out the issue will be greatly appreciated.

谢谢.

推荐答案

尝试针对从活动B开始时,您将使用TaskStackBuild来构建后堆栈并获取PendingIntent.

You will have use TaskStackBuild to build the back stack and get the PendingIntent when starting on Activity B.

检查来自Android设计模式的视频解释起来很简单.

这篇关于向上导航不启动父级活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆