Android:如果应用从另一个应用打开,则 launchMode singleTop 不工作
Android: launchMode singleTop not working if app opened from another app
我有一个应用程序,如果从另一个应用程序(例如通过 playstore)启动,它会出现异常。它不是恢复到已经存在的 Activity
,而是作为一个新实例重新启动。
我有:
- 在
manifest.xml
中用 launchMode="singleTop"
声明每个 activity
- 我用
launchMode=singleTask
试过同样的方法,但它有相同的行为
- 在每个
Intent
上使用额外的 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
开始新的 Activity
onNewIntent()
尚未在 运行 实例中调用
我使用以下代码从另一个应用程序启动我的应用程序(有和没有额外的 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
我有一个应用程序,如果从另一个应用程序(例如通过 playstore)启动,它会出现异常。它不是恢复到已经存在的 Activity
,而是作为一个新实例重新启动。
我有:
- 在
manifest.xml
中用 - 我用
launchMode=singleTask
试过同样的方法,但它有相同的行为 - 在每个
Intent
上使用额外的intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
开始新的Activity
onNewIntent()
尚未在 运行 实例中调用
launchMode="singleTop"
声明每个 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