Android:如果应用程序从其他应用程序

时间:2015-08-04 15:28:54

标签: 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);

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

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

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

2 个答案:

答案 0 :(得分:2)

请尝试使用singleTask而不是singleTop用于SplashScreenActivity。 根据{{​​3}}

“系统在新任务的根目录下创建活动并将意图路由到该任务。但是,如果活动的实例已经存在,系统会通过调用其onNewIntent()将意图路由到现有实例方法,而不是创建一个新方法。“

答案 1 :(得分:1)

经过一些研究后,我在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