单击通知时如何保留当前的后退堆栈(或任务)? [英] How to preserve current back stack (or task) when notification is clicked?

查看:129
本文介绍了单击通知时如何保留当前的后退堆栈(或任务)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我创建一个通知,该通知以 Details Activity 开始.我想将此活动添加到当前任务(或回退堆栈)的顶部.例如,我希望应用程序任务(后向堆栈)的行为如下:

In my application, I create a notification which starts Details Activity. I want to add this activity to top of current task (or back stack). For example I expect application task (back stack) to behave like this:

但是我明白了:

我还没有使用FLAG_ACTIVITY_CLEAR_TASKFLAG_ACTIVITY_NEW_TASK标志.我该怎么办?

I have not used FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flags. What should I do?

第一张图片只是一个例子.我认为问题的标题是完全明确的.我想在当前堆栈的顶部添加 Details Activity ,而不是从一个新任务开始.

First picture is just an example. I think the the question's title is completely explicit. I want to add Details Activity on top of current stack, and not to start with a new task.

这就是我创建PendingIntent的方式:

    // Details activity intent
    Intent intent = new Intent(context, DetailsActivity.class);
    intent.putExtra(Com.KEY_ID, event.getId());
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

这是清单:

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name_system"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme.NoActionBar">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".NoteActivity"
        android:label="@string/app_name_system"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateHidden" />

    <activity
        android:name=".DetailsActivity"
        android:launchMode="singleTop"
        android:label="@string/app_name_system"
        android:theme="@style/AppTheme.NoActionBar" />

推荐答案

我找到了此链接:

I found this link: Preserving Navigation when Starting an Activity. Although it does not provide the exact solution for my question, but it produces the desired result.

该链接描述了我们应该以不同的DetailsActivity rel ="nofollow noreferrer">亲和力 .

The link describes that we should start DetailsActivity in a new task with a different affinity.

清单:

    <activity
        android:name=".DetailsActivity"
        android:excludeFromRecents="true"
        android:label="@string/app_name_system"
        android:launchMode="singleTop"
        android:taskAffinity=""
        android:theme="@style/AppTheme.NoActionBar" />

PendingIntent创建:

    Intent intent = new Intent(context, DetailsActivity.class);
    intent.putExtra(Com.KEY_ID, event.getId());
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

这篇关于单击通知时如何保留当前的后退堆栈(或任务)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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