Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: navigating up to the parent activity recreates the parent activity

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>

From SearchFragment, I start PersonActivity like so:

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

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.

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?

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

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

like image 300
James Avatar asked Jan 14 '14 01:01

James


1 Answers

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"

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.

like image 118
David Wasser Avatar answered Nov 09 '22 05:11

David Wasser