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

Android: launchMode singleTop not working if app opened from another app

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

我有:

我使用以下代码从另一个应用程序启动我的应用程序(有和没有额外的 addFlag()

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("my.package.name");
launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(launchIntent);

我的启动器-Activity 是一个 SplashScreenActivity,它会启动 MainActivity 如果用户使用以下代码登录并获得 finished()

 Intent intent = null;
 intent = new Intent(SplashScreenActivity.this, HomeActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
 startActivity(intent);
 finish();

我错过了什么?欢迎任何建议!

请尝试使用 singleTask 而不是 SplashScreenActivity 的 singleTop。 根据 http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

"The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one."

经过更多研究,我在 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;
        }
    }
    //...

}

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