Subtitle is lost when navigating up


#1

Upon completing chapter 16, I noticed that an interesting thing:
Suppose I show the subtitle on the CrimeListFragment and then open/add a crime, if I return to the CrimeListFragment using the BACK button, the subtitle is preserved because the existing CrimeListFragment is accessed.
However, if I return to the CrimeListFragment using the UP button, the subtitle is NOT preserved. This seems to be because the existing CrimeListfragment is destroyed and a new CrimeListFragment is created.

Apparently, there was an earlier post that inquired about the same problem. I tried the suggestion. However, it seems that the old CrimeListFragment is still destroyed and a new CrimeListFragment is created with a savedInstanceState=null.

Upon further experimenting, I changed the flag from SINGLE_TOP to CLEAR_TOP. Then it seems to work. Here is the change made:

Replace the following (in CrimeFragment.java):
if (NavUtils.getParentActivityName(getActivity()) !=null) {
NavUtils.navigateUpFromSameTask(getActivity());
}

with:
if (NavUtils.getParentActivityName(getActivity()) !=null) {
Log.d(TAG, “going to return to parent activity using UP”);

Intent i = NavUtils.getParentActivityIntent(getActivity());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(getActivity(), i);

}

My question is:
What is the difference between FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP?


#2

I found the same thing. With the book code, I get the following in LogCat when the Android icon (the one added by us) is pressed to go back to the CrimeListFragment:

06-26 01:50:30.273: D/CrimeListFragment(1492): onDestroy() 06-26 01:50:30.273: D/CrimeListFragment(1492): onDetach() 06-26 01:50:30.515: D/CrimeListFragment(1492): onAttach() 06-26 01:50:30.515: D/CrimeListFragment(1492): onCreate() 06-26 01:50:30.523: D/CrimeListFragment(1492): onCreateView() 06-26 01:50:30.543: D/CrimeListFragment(1492): onResume() 06-26 01:50:31.143: D/CrimeListFragment(1492): onCreateOptionsMenu()

If the Android device back-button is pressed instead, or the solution by AnnRos is used:

06-26 01:54:27.452: D/CrimeListFragment(1492): onResume()

The code:

[code]@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (NavUtils.getParentActivityName(getActivity()) != null) {
NavUtils.navigateUpFromSameTask(getActivity()); // BOOK CODE

                    // AnnRos Code:
		//Intent i = NavUtils.getParentActivityIntent(getActivity());
		//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		//NavUtils.navigateUpTo(getActivity(), i);
	}
	return true;
default:
	return super.onOptionsItemSelected(item);
}

}[/code]

Is there a way to also accomplish this in the manifest?

Thanks!


#3

I also found this works:

if (NavUtils.getParentActivityName(getActivity()) != null) { getActivity().finish(); }


#4

Yeah, that is an oversimplification in the text. The default launch mode will force your activity to restart, even with the clear top flag set.

I haven’t tested it, but you should be able to fix this behavior by switching to the singleTop launch mode in your manifest (as described in the Search chapter in the text).


#5

I can confirm that changing the launchMode for the activity CrimeListActivity to “singleTop” does fix this behavior with no coding changes.