【问题标题】:How to start Activity with pending intent and clear all backstack?如何以挂起的意图启动 Activity 并清除所有后台堆栈?
【发布时间】:2021-02-05 20:25:17
【问题描述】:

我有不同片段的“HomeActivity”。单击pendingIntent(HomeActivity)时,我想清除HomeActivity中的所有backstack。如果任何其他片段打开,当我点击 pendingIntent 时它应该会被清除。

Andoidmanifest.XML

<activity
        android:screenOrientation="portrait"
        android:launchMode = "singleTask"
        android:taskAffinity=""
        android:excludeFromRecents="true"
        android:name=".HomeActivity"
        android:label="@string/title_activity_home"
        android:theme="@style/AppTheme.NoActionBar" />

通知意图代码如下:

 Intent notifyIntent = new Intent(this, SplashScreen.class);   //SplashScreen is first activity
                // Set the Activity to start in a new, empty task
                notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent pendingIntent = PendingIntent.getActivity(context, id, notifyIntent , PendingIntent.FLAG_UPDATE_CURRENT);

                Common.showNotification(this, new Random().nextInt(),
                        dataRecv.get(Common.NOTI_TITLE),
                        dataRecv.get(Common.NOTI_CONTENT),
                        pendingIntent);

【问题讨论】:

  • 你为什么使用singleTask启动模式?
  • 我通过添加解决了这个问题 (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); SplashScreen 调用我的主要活动时的标志。
  • 好的,但是你仍然需要小心使用特殊的启动模式,比如singleTasksingleInstance。一般来说,您不应该使用它们,因为它们产生的问题比解决的问题多。

标签: android android-intent notifications android-notifications android-pendingintent


【解决方案1】:

你需要调用下面的方法来清除所有的backstack

finishAffinity();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2013-06-11
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2012-10-08
    相关资源
    最近更新 更多