Navigation / Notification / TaskStackBuilder

Task Stack

There are often occasions when we need to take the user to a specific Activity and / or piece of content within an app. A typical use-case for this is a notification where tapping on the notification should launch the app directly to a specific place. In the recent series or Oreo Notification Channels we looked at a mocked messaging app, and for such an app it would be good if tapping on the notification could launch the app and take the user directly to the message in question. While this is easy enough to do, the back navigation behaviour from that newly launched Activity can be somewhat confusing to the user. In this article we’ll look at these behaviours, and also some strategies that we can adopt to control this navigation behaviour, and how we can tailor it to provide more a more natural behaviour.

Before we get stuck in, it is worth pointing out that although we’ll be working on the same code as we used for Oreo Notification Channels, the issues and techniques that we’ll cover will work back to Android 4.1 Jelly Bean (API 16) if we use the native version, or earlier if we use the v4 core-utils library support library.

The Oreo Notifications app doesn’t actually support the display of individual messages as it is simply a test bed to show how we can create different kinds of Notification. So we’ll begin by adding a second Activity to the app so that we can have some simple navigation. We’ll add a button to the existing Activity, and then ensure that we get the correct back an up behaviour from the second Activity:

Next we’ll add SecondActivity to the Manifest:

By adding parentActivityName and the meta-data we configure the navigation hierarchy. In this case, hitting back will return to the MainActivity. Not only is it good practice to do this, but it will also be really helpful later on.

Finally we implement SecondActivity:

For simplicity I have made both back and home behave the same way, but different apps may have different requirements here, so nothing that we’ll cover here is dependent on this kind of behaviour.

So the basic behaviour that we’ll get here is if we tap on the button in MainActivity, SecondActivity will be launched. If we then hit either “back” or “up” we’ll return to the MainActivity.

So with that in place, we’ll pretend that SecondActivity would display a specific message (it won’t but, we can pretend, right?). When we create our Notification

With this in place we can now take a look at how the back navigation works. If our app isn’t actually running, then hitting “back” simply closes SecondActivity and returns us to wherever we were previously:

But this behaviour is different if the app is already running. If the app is currently on running on MainActivity, hitting “back” from the newly launched SecondActivity will actually go back to MainActivity even though it wasn’t actually resumed when SecondActivity was launched. This is because of the navigation that we added to the manifest:

However, if SecondActivity is actually at the top of the task stack in the backgrounded app, then hitting “back” from the newly launched SecondActivity will actually return to the previous SecondActivity:

This kind of inconsistency can be really frustrating for the user, so it is important that we think very carefully about what is the correct behaviour, and ensure that it remains consistent for all permutations of whether the app is already running in the background, and the current state of the backgrounded app.

While there are a number of approaches that we could take, it is often best for the app to behave consistently irrespective of how the use got to SecondActivity. So irrespective of whether the user reached SecondActivity by tapping on a Notification, or by navigating there from MainActivity, then “back” will always return to MainActivity, even if the app wasn’t previously running in the background. We already have that behaviour then the user navigates there from within the app, so we need to recreate the back stack when SecondActivity is launched from a Notification.

The key to achieving this is to use TaskStackBuilder which was introduced in API 16 but is also available in the v4 core utils support library. This can work some real magic if we use correctly declare our navigation hierarchy in the Manifest (as we did earlier – I said it would make life easier!). Let’s replace our PendingIntent creation to use this:

The first line of getContentIntent() creates a TaskStackBuilder instance, and runs the nest three commands on that instance.

The second line constructs the back stack for us. This is where all of the magic happens: the addParentStack() function creates a back stack from the navigation information we provided for the Activity class that we pass in as the argument. Earlier we defined the parent Activity for SecondActivity to be MainActivity and this function will construct this back stack for us. Even if we had multiple layers of navigation, in other words if the parent of this Activity was a different Activity, and that Activity has a declared parent of MainActivity, then the correct back stack would be created. The naming is a little confusing here, because it is easy to wrongly infer that we should be providing the class of the parent Activity here, but we actually need to provide the class of the Activity which will be created by the PendingIntent.

The third line constructs the Intent to launch SecondActivity. We set FLAG_ACTIVITY_SINGLE_TOP here to ensure that if SecondActivity is already top of the task stack then we don’t create a duplicate.

The fourth line creates the PendingIntent which will launch SecondActivity with a complete back stack, and this PendingIntent is what gets returned from this function.

As a result of doing this the in-app back navigation and that which is used when launching from a Notification are both constructed from the parent hierarchy which we declared in the Manifest, so we get consistent behaviour throughout:

TaskStackBuilder is a really simple API to use but, provided we correctly declare our navigation hierarchy in the Manifest, it can do some really powerful things for surprisingly little effort. Of course it is not just limited to Notifications as we can get correct back stack implementation anywhere that a PendingIntent is used to launch an Activity.

The source code for this article is available here.

© 2017, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Task Stack by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *