Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当我从自定义快捷方式启动活动时,如何销毁所有以前的活动?_Android_Android Activity_Shortcut - Fatal编程技术网

Android 当我从自定义快捷方式启动活动时,如何销毁所有以前的活动?

Android 当我从自定义快捷方式启动活动时,如何销毁所有以前的活动?,android,android-activity,shortcut,Android,Android Activity,Shortcut,我的步骤: activity A: android:launchMode="singleTop" activity B: android:launchMode="singleTop" and android:exported="true" 启动器快捷启动活动A 单击主页按钮 自定义快捷方式启动活动B 单击后退按钮-显示活动A 但当我单击“自定义快捷方式”时,我希望所有的旧活动都关闭。我该怎么做 清单: activity A: android:launchMode="singleTop" act

我的步骤:

activity A: android:launchMode="singleTop"
activity B: android:launchMode="singleTop" and android:exported="true"
  • 启动器快捷启动活动A
  • 单击主页按钮
  • 自定义快捷方式启动活动B
  • 单击后退按钮-显示活动A
  • 但当我单击“自定义快捷方式”时,我希望所有的旧活动都关闭。我该怎么做

    清单:

    activity A: android:launchMode="singleTop"
    activity B: android:launchMode="singleTop" and android:exported="true"
    
    java:

    private void setShortCut() {
      Intent shortcutIntent = new Intent(getApplicationContext(), A.class);
      shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
      shortcutIntent.setAction(Intent.ACTION_MAIN);
      Intent intent = new Intent();
      intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
      intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "test");
      intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, ((BitmapDrawable)imgLogo.getDrawable()).getBitmap());
      intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
      sendBroadcast(intent);
    }
    
    从官方文件


    “每当用户再次启动其任务(在主屏幕上选择任务)时,是否应关闭(完成)该活动的现有实例”-如果应关闭,则为“真”;如果不应关闭,则为“假”。默认值为“假”。

    我建议您也使用自定义快捷方式启动
    活动a
    ”,但是在
    Intent
    中传递额外的extra。在
    ActivityA.onNewIntent()
    ActivityA.onCreate()
    中,您可以检查是否存在额外的内容,如果存在,请启动
    ActivityB
    并调用
    finish()
    。这应该给你想要的行为

    例如,在创建自定义快捷方式时添加以下内容:

    shortcutIntent.putExtra("customShortcut", true);
    
    并将此代码放入
    ActivityA的
    onCreate()
    onNewIntent()

    if (getIntent().hasExtra("customShortcut")) {
        Intent intent = new Intent(this, B.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity();
        finish();
        return;
    }
    

    您是否尝试过android:noHistory=“true”?
    launchMode=“singleTop”
    工作正常。您只有一个ActivityA实例和一个ActivityB实例。如果我设置
    android:noHistory=“true”
    ,那么每次为ActivityA执行
    finish()
    方法时,都会执行
    singleTop
    @Kgrover,您无法获得想要的行为。不仅当我从自定义快捷方式启动应用程序时。@David Wasser,你完全正确。谢谢你的解释!!!你对取消之前所有的活动有什么想法吗?你不想要4。单击后退按钮-向我显示活动A右键?在这种情况下,单击启动程序快捷键,我将始终看到活动A。不仅在自定义快捷方式后单击。我希望只有在自定义快捷方式启动活动B后,我的所有旧活动才会关闭。现在我正在使用此技术。但用户总是看到ActivityA第二次启动wile ActivityB。所以我想找到一种方法来关闭我所有的旧活动。好吧,如果ActivityB需要那么长时间才能启动,那么关闭所有的旧活动可能没有帮助。如果从根活动中删除'launchMode=“singleTop”`并从启动意图中删除
    Intent.FLAG\u activity\u SINGLE\u TOP
    ,这将导致启动时重新创建
    ActivityA
    。也许这会有帮助。