android - onBackPressed() 到父 Activity

标签 android android-activity parent-child parent

我有一个包含很多 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/

相关文章:

angular - (如何)我可以从 NgbModal 模态调用父组件函数吗?

android - 使用 LinearLayout 和权重更改可见性

android - 警报管理器立即触发

android - 如何将二维数组从一个 Activity 传递到另一个 Activity ?

java - 将来自 getCallingActivity 的 Activity 与另一个进行比较

Java:关于如何从基类继承工厂方法的最佳实践

android - FragmentTabHost 选项卡的内容未出现

android - ViewPager 的 View 持有者模式?

java - 在 Eclipse Android 中添加新 Activity

c# - 删除游戏对象的最后一个 child 的简单方法