如何 return 到 activity 关闭中间活动?
How to return to an activity closing the in between activities?
我需要返回我的应用程序的主 activity,但为此,我需要关闭当前 activity 和主 activity 之间的所有活动.
我试过这个:
Intent intent = new Intent(getActivity(), MainMenuActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity( intent );
问题在于这样做,主activity也被关闭并重新打开,调用主命运activity中的onDestroy()。我需要在不关闭并重新打开它的情况下返回它。如何实现?
您是否尝试将 launchMode
设置为 MainActivity
。请注意,这应该针对特定 Activity
内的所有交易完成。检查 the options for from documentation。
An instruction on how the activity should be launched. There are four
modes that work in conjunction with activity flags (FLAG_ACTIVITY_*
constants) in Intent objects to determine what should happen when the
activity is called upon to handle an intent. They are:
"standard"
"singleTop"
"singleTask"
"singleInstance"
FLAG_ACTIVITY_CLEAR_TOP
上的 documentation 说
The currently running instance [...] 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().
对于您的情况,我假设您的 MainMenuActivity
具有 "multiple" 启动模式。由于你没有设置FLAG_ACTIVITY_SINGLE_TOP
,所以完成并重新启动。
所以如果你使用
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);
然后根据文档,MainMenuActivity
实例将保持其状态并调用 onNewIntent()
。
注意:您可以使用 setIntent()
使 Intent
传递到 onNewIntent()
"the" Intent
Activity
我需要返回我的应用程序的主 activity,但为此,我需要关闭当前 activity 和主 activity 之间的所有活动.
我试过这个:
Intent intent = new Intent(getActivity(), MainMenuActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity( intent );
问题在于这样做,主activity也被关闭并重新打开,调用主命运activity中的onDestroy()。我需要在不关闭并重新打开它的情况下返回它。如何实现?
您是否尝试将 launchMode
设置为 MainActivity
。请注意,这应该针对特定 Activity
内的所有交易完成。检查 the options for from documentation。
An instruction on how the activity should be launched. There are four modes that work in conjunction with activity flags (FLAG_ACTIVITY_* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent. They are:
"standard"
"singleTop"
"singleTask"
"singleInstance"
FLAG_ACTIVITY_CLEAR_TOP
上的 documentation 说
The currently running instance [...] 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().
对于您的情况,我假设您的 MainMenuActivity
具有 "multiple" 启动模式。由于你没有设置FLAG_ACTIVITY_SINGLE_TOP
,所以完成并重新启动。
所以如果你使用
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);
然后根据文档,MainMenuActivity
实例将保持其状态并调用 onNewIntent()
。
注意:您可以使用 setIntent()
使 Intent
传递到 onNewIntent()
"the" Intent
Activity