我有一个应用程序,如果从另一个应用程序(例如通过 playstore)启动,它会出现异常。它不是恢复到已经存在的 Activity
,而是作为一个新实例重新启动。
我有什么:
- 在
manifest.xml
中使用launchMode="singleTop"
声明每个 Activity - 我用
launchMode=singleTask
做了同样的尝试,但它有相同的行为 - 在每个启动新
Activity
的Intent
上使用额外的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/