Android:如果从另一个应用程序打开应用程序,则 launchMode singleTop 不起作用

lon*_*ngi 5 android android-launcher

我有一个应用程序,如果从另一个应用程序启动(例如通过 Playstore),它会出现错误。它不会恢复到已经存在的实例Activity,而是作为新实例重新启动。

我拥有的:

  • launchMode="singleTop"声明每项活动manifest.xml
  • 我尝试了同样的操作 launchMode=singleTask,但它具有相同的行为
  • intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)在每个Intent开始新的内容时使用额外的Activity
  • onNewIntent()不在已经运行的实例中调用

我使用以下代码从另一个应用程序启动我的应用程序(带或不带附加addFlag()

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("my.package.name");
launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(launchIntent);
Run Code Online (Sandbox Code Playgroud)

我的启动器活动是一个SplashScreenActivity,如果MainActivity用户使用以下代码登录并获取finished()

 Intent intent = null;
 intent = new Intent(SplashScreenActivity.this, HomeActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
 startActivity(intent);
 finish();
Run Code Online (Sandbox Code Playgroud)

我缺少什么?有什么建议欢迎留言!

lon*_*ngi 6

经过更多研究后,我在SplashScreenAvtivity:onCreate()

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!isTaskRoot())
    {
        String intentAction = getIntent().getAction();
        if (getIntent().hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
            finish();
            return;
        }
    }
    //...
Run Code Online (Sandbox Code Playgroud)

}

如果应用程序已经在运行,这将关闭 SplashScreenActivity。这适用于所有launch-modes