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

标签 android android-launcher

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

我有什么:

  • manifest.xml 中使用 launchMode="singleTop" 声明每个 Activity
  • 我用 launchMode=singleTask 做了同样的尝试,但它有相同的行为
  • 在每个启动新 ActivityIntent 上使用额外的 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
  • onNewIntent() 没有在已经运行的实例中调用

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

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

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

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

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

最佳答案

经过更多研究,我在 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。这适用于所有启动模式

关于Android:如果应用程序从另一个应用程序打开,则 launchMode singleTop 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31813322/

相关文章:

Android:我应该在哪里寻找证书吊销列表?

android - 如何在 EditTextPreference 的右侧显示当前值?

android - 将值从一个 Activity 检索到另一个 Activity 时导致应用程序崩溃

android - Android O 中的图标

android - 自动阻止应用程序从启动器启动

android - Android 中的 Firebase 实时数据库最大缓存大小

android - 如何只录制1分钟的音频?

android - 三星Touchwiz始终在Android 4.1.1中启动时提示默认应用程序

android - 如何从应用程序更改启动器图标及其标签

java - 在我的启动器应用程序内按主页按钮转到主屏幕