で始まるアプリケーションがありますSplashScreenActivity
。その後、 aLoginActivity
が表示されるか、ユーザーがすでにログインしている場合は aMainActivity
が表示されます。アプリケーションがすでに実行されている場合はSplashScreenActivity
、次のコマンドで終了します
//SplashScreenActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Adding this check for following cases
if (!isTaskRoot())
{
String intentAction = getIntent().getAction();
if (getIntent().hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
finish();
return;
}
if(getIntent().getCategories().contains(GCMIntentService.INTENT_CATEGORY_GH_NOTIFICATION)){
finish();
return;
}
}
問題発生
PlayStore などの別のアクティビティからアプリケーションを起動すると、既に実行されている場合は適切なアクティビティで再開されます。これは、Intent
2番目のアプリ内で再現するために使用しています
//AnotherApplication.apk
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("my.package.name");
startActivity(launchIntent);
ただし、このアクションはどういうわけかバックスタックを壊しています。の backpress でアプリケーションを閉じる代わりに、アプリケーションをMainActivity
再起動します。
//MainActivity.class
@Override
public void onBackPressed() {
if (getNavDrawerMain().isDrawerOpen()) {
getNavDrawerMain().closeDrawer();
} else {
closeApp();
}
}
protected void closeApp() {
if (doubleBackToExitPressedOnce) {
//super.onBackPressed(); //i tried both, but behaviour is the same
finish();
return;
}
this.doubleBackToExitPressedOnce = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run()
doubleBackToExitPressedOnce = false;
}
}, 500);
}
私はブレークポイントを使用し、 MainActivity:onDestroy() が呼び出されることを発見しましたが、ホームスクリーンへのアプリケーションを再開する代わりに、常に再起動し、理由がわかりません。
私は次のことを試しました: - と のような異なる起動モードを使用singleTask
しsingleInstance
ましたが、違いはありませんでした。onNewIntent
が呼び出されますが、 を呼び出すfinish
とHomeActivity
再起動します - 以下に示すように、試してみmoveTaskToBack(true)
ましたが、アクティビティも再起動しています (そして、アプリを BackStack に移動するのではなく、実際に閉じたいと考えています)