Android:导航到堆栈中的第一个 MainActivity
Android: navigate to first MainActivity in stack
默认情况下 Android 创建新的 Activity 实例并在用户返回时关闭它。这使得堆栈。
在尝试使用与 standard
不同的 launchMode
时,我们发现在某些设备上启动这些活动时会出现不需要的动画。
在我们的应用程序中有一个搜索,按类别查看,相关和最后的详细信息,
可以将用户带到厚堆栈。
是否有简单的方法来添加操作 "Return to home" 以丢弃所有堆栈到 MainActivity?
是的,向您的主页启动一个新 Intent Activity,如下所示:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
overridePendingTransition(R.anim.fade);
finish();
使用该标志,您可以清理堆栈和主页Activity,它现在是您应用程序的主要activity。
发现于Go back to the First/Main activity without reloading it
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
来自文档
For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.
The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
所以如果没有 FLAG_ACTIVITY_SINGLE_TOP 它将导致重新创建 MainActivity。
是的,向您的主页启动一个新 Intent Activity,如下所示:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
overridePendingTransition(R.anim.fade)
finish();
使用该标志,您可以清理堆栈和主页Activity,它现在是您应用程序的主要activity。
默认情况下 Android 创建新的 Activity 实例并在用户返回时关闭它。这使得堆栈。
在尝试使用与 standard
不同的 launchMode
时,我们发现在某些设备上启动这些活动时会出现不需要的动画。
在我们的应用程序中有一个搜索,按类别查看,相关和最后的详细信息, 可以将用户带到厚堆栈。
是否有简单的方法来添加操作 "Return to home" 以丢弃所有堆栈到 MainActivity?
是的,向您的主页启动一个新 Intent Activity,如下所示:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
overridePendingTransition(R.anim.fade);
finish();
使用该标志,您可以清理堆栈和主页Activity,它现在是您应用程序的主要activity。
发现于Go back to the First/Main activity without reloading it
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
来自文档
For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.
The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().
所以如果没有 FLAG_ACTIVITY_SINGLE_TOP 它将导致重新创建 MainActivity。
是的,向您的主页启动一个新 Intent Activity,如下所示:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
overridePendingTransition(R.anim.fade)
finish();
使用该标志,您可以清理堆栈和主页Activity,它现在是您应用程序的主要activity。