Up Button and how to keep Subtitle count displayed

Edit your androidmanifest.xml file to include “android.launchMode=“singleTask””. The 3rd edition pg 265 says activity gets completely re-created using the UP button. This seems to work. According to documentation it keeps the activity around. Edited to add example xml.

<?xml version="1.0" encoding="utf-8"?>

    <activity android:name=".CrimeListActivity"
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
    <activity android:name=".CrimePagerActivity"

Are their any downsides to this approach?

Hi @Willscott and @JuanRomo,

In my test, when singleTask is used, the subtitle can be kept. However, the recyclerView is not refreshed to show the latest added crime entry when up button is clicked and CrimeListFragment is back.

Not sure whether you have the same issue.

I paste the git link in case you need to check the source code. Thanks.

Hi @Willscott and @JuanRomo,

As double checked, the solution to my issue is listed in the following link. It’s not related to the launch mode.

@JuanRomo For your question, if an Activity’s launch mode is set to be singleTask, since activities with “singleTask” or “singleInstance” launch modes can only be at the root of a task, re-parenting is limited to the “standard” and “singleTop” modes.

By the way, I’ve tested three scenarios as follows. I think launch mode singleTop for the parent activity is a better choice.

  1. Click Back Button in CrimePagerActivity
    The CrimeListFragment’s onDestroyView() will not be called. i.e. its state is preserved.

  2. Click Up Button in CrimePagerActivity with CrimeListActivity in singleTop launch mode.
    The CrimeListFragment’s onDestroyView() will not be called. i.e. its state is preserved.
    Same behavior as the previous one.

  3. Click Up Button in CrimePagerActivity with CrimeListActivity in standard launch mode.
    The CrimeListFragment’s onDestroyView() will be called and the fragment will be reestablished. i.e. its state is lost.

The background details can be found in the following guide link.


Similarly, if you navigate up to an activity on the current stack, the behavior is determined by the parent activity’s launch mode. If the parent activity has launch mode singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP), the parent is brought to the top of the stack, and its state is preserved. The navigation intent is received by the parent activity’s onNewIntent() method. If the parent activity has launch mode standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent.

@cstewart Could you help correct me if I miss something from the guide?

By the way, according to the description in section HOW HIERARCHICAL NAVIGATION WORKS of Chapter 13, it says two points.

  1. When the user navigates up from CrimePagerActivity, an intent with flag Intent.FLAG_ACTIVITY_CLEAR_TOP is created.

  2. FLAG_ACTIVITY_CLEAR_TOP tells Android to look for an existing instance of the activity in the stack, and, if there is one, pop every other activity off the stack so that the activity being started will be top-most (Figure 13.12).

This behavior looks like the launch mode of the parent activity is singleTop.

However, according to the description in section “JUST ONE MORE THING…” of Chapter 13, it says the following point.

  1. An unfortunate side effect of the way hierarchical navigation is implemented in Android is that the activity that you navigate up to will be completely re-created from scratch. This means that any instance variables will be lost, and it also means that any saved instance state will be lost as well. This parent activity is seen as a completely new activity.

This behavior looks like the launch mode of the parent activity is standard.

Could you have a double check?

Thank you very much and I’m very enjoying the fantastic book. :wink:

1 Like


Using the source code for Ch 13 downloaded from the web site and editing the androidmanifest.xml to add

  <activity android:name=".CrimeListActivity"

both the up button and back button show a correctly updated toolbar count and correct display in the window (total number displayed matches toolbar number). Note the back button must be pressed twice once to close the keyboard.

Android Studio 2.3 and Nexus 5 API 22

Check out Android Architecture Components - just started going through “codelab” but they seem to have addressed these problems with ViewModel and much more.


Hi Willscott,

In this project, “singleTask” and “singleTop” are both valid launch modes.

According to the guide, I think singleTop is a better choice.

As shown in the table above, standard is the default mode and is appropriate for most types of activities. SingleTop is also a common and useful launch mode for many types of activities. The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.

Making mSubtitleVisible a static variable solves the issue.
In CrimeListFragment change
private boolean mSubtitleVisible;
private static boolean mSubtitleVisible;