安卓导航到父活动再现了父活动 [英] Android: navigating up to the parent activity recreates the parent activity

查看:199
本文介绍了安卓导航到父活动再现了父活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有一个孩子PersonActivity一个SearchAcvitity。每个都是FragmentActivity的。这里是我的清单文件:

I have a SearchAcvitity which has a child PersonActivity. Each are FragmentActivity's. Here's my manifest file:

<activity android:name=".SearchActivity" android:label="@string/search_title">
</activity>


<activity android:name=".PersonActivity" android:label="@string/person_title"
            android:parentActivityName=".SearchActivity" >
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".SearchActivity" />
</activity>

从SearchFragment,我开始PersonActivity像这样:

From SearchFragment, I start PersonActivity like so:

Intent i = new Intent(getActivity(), PersonActivity.class);
startActivity(i);

这增加了单独面向左插入符一起在PersonFragment的应用程序图标。当我preSS该图标,它再现SearchAcvitiy(和片段),因此它的previous状态会丢失。

This alone adds the left-facing caret alongside the app icon in PersonFragment. When I press this icon, it recreates SearchAcvitiy (and fragment), so its previous state is lost.

在我,而不是preSS从PersonFragment后退按钮,它通过onResume追溯到SearchActivity的SearchFragment(...)和它的状态是否完好。我怎样才能做到这一点的跟进行动?

When I instead press the back button from PersonFragment, it goes back to SearchActivity's SearchFragment via onResume(...) and its state is intact. How can I make this happen with the up action?

请注意,我不作在PersonFragment此调用:

Please note that I don't make this call in PersonFragment:

getActivity()getActionBar()setDisplayHomeAsUpEnabled(真);

getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

推荐答案

向上导航与启动 Intent.FLAG_ACTIVITY_CLEAR_TOP 父活动。该标志的标准行为是完成是在任务堆栈父活动的顶级活动包括父活动本身,然后启动父活动的新实例**。如果你想恢复父活动的现有实例,然后就可以设置在清单父活动如下:

UP navigation launches the parent Activity with Intent.FLAG_ACTIVITY_CLEAR_TOP. The standard behaviour of this flag is to finish all Activities that are on top of the parent Activity in the task stack including the parent Activity itself and then launch a new instance of the parent Activity**. If you want to resume the existing instance of the parent Activity, then you can set the following in the manifest for the parent Activity:

android:launchMode="singleTop"

在CLEAR_TOP和SINGLE_TOP一起使用,这将恢复父活动的现有实例。在这种情况下,的onCreate()不会呼吁恢复父活动,而是 onNewIntent()将叫吧。

When CLEAR_TOP and SINGLE_TOP are used together, this will resume an existing instance of the parent Activity. In this case, onCreate() will not be called on the resumed parent Activity, but instead onNewIntent() will be called instead.

这篇关于安卓导航到父活动再现了父活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆