我有一个包含很多 Activity 的应用程序。在一个 Activity 中,我想在按下后退按钮时返回到父 Activity 。 (前一个 Activity 和父 Activity 不一样)。我如何使用 onBackPressed 执行此操作? 提前致谢。
最佳答案
查看 this page about navigation从文档中。这是他们要说的:
To navigate up when the user presses the app icon, you can use the NavUtils class's static method, navigateUpFromSameTask(). When you call this method, it finishes the current activity and starts (or resumes) the appropriate parent activity. If the target parent activity is in the task's back stack, it is brought forward. The way it is brought forward depends on whether the parent activity is able to handle an onNewIntent() call:
If the parent activity has launch mode , or the up intent contains FLAG_ACTIVITY_CLEAR_TOP, the parent activity is brought to the top of the stack, and receives the intent through its onNewIntent() method.
If the parent activity has launch mode , and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP, the parent activity is popped off the stack, and a new instance of that activity is created on top of the stack to receive the intent.
编辑:
首先将父 Activity 添加到 list 中:
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
然后在按下后退按钮时调用 NavUtils.navigateUpFromSameTask(this);
。
关于android - onBackPressed() 到父 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24959265/