【问题标题】:Add Activity on top of existing backstack when launched from notification从通知启动时在现有 backstack 上添加 Activity
【发布时间】:2018-07-05 08:16:19
【问题描述】:

我有活动 A、B 和 C C 从通知启动。

但是,当从通知启动时,backstack 会被清除。我知道 TaskStackBuilder 并指定了返回意图,但这将是一个硬编码的返回堆栈。

如果 C 在 A 之后启动,则后按应该回到 A 如果 C 在 A>B 之后启动,则后按应该返回 B,然后在第二次后按时返回 A。

如何保留当前的返回堆栈并在其上添加?

【问题讨论】:

    标签: android android-activity android-notifications back-stack


    【解决方案1】:

    根据您的需要,将 Manifest.xml 中所有可能的目标活动的 launchMode 设置为“singleTop”或“singleTask”:

    <activity
        android:name=".YourActivity"
        android:launchMode="singleTop"> 
    

    不同的启动模式很好解释here

    【讨论】:

    • 1) 是否保留现有的活动堆栈?我正在从通知中启动活动。 2) 我应该将目标活动声明为单顶活动吗?
    • 我已将目标活动设置为 singleTask。没有帮助。当您从通知中启动活动时,Android 会清除后台堆栈。
    • 我将我的pendingintent定义为:PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);其中 notifyIntent 是我的目标活动意图
    猜你喜欢
    • 2016-11-16
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多