MALE SPEAKER: You tap the launcher icon
for your app, your app launches.
Sounds easy, right?
What's happening behind the scenes
is a little more involved.
It turns out the activities in your app
don't live in a vacuum, but instead exist
as part of one or more tasks.
Now, a task, in and of itself, isn't very complicated.
It's simply a stack of activities.
As you call start activity, that pushes a new activity
onto the task's back stack.
The Back button reverses this, calling finish
on the current activity, popping it from the stack,
and taking the user back to where they were.
Hence the name.
This symmetric push-pop model also applies to fragments.
When specifying a fragment transaction,
you can call add to back stack to add the fragment
transaction to the back stack.
Then, when the user hits the Back button, instead of
your activity being finished, the fragment transaction
is reversed.
Only when there are no more fragment transactions
will the Back button finish your activity.
Now, if instead of hitting the Back button,
you were to hit the Home button, the whole task
is put in the background.
If you then tap the launcher icon,
the system brings the task back to the foreground,
right where you left it.
The same thing happens if you select
the app's task from the overview screen,
the task is brought to the foreground.
For many apps, this default behavior is all you would need.
No touchy.
Nice, consistent, symmetric operations.
But there are two specific cases where you might
want to do something special.
First, if you're calling start activity with the activity
you're already on.
This might be the case where you're on a search results
page, and search again.
Instead of piling up activity after activity,
and requiring multiple presses of the Back button,
you can add a launch mode of single top
to your activities manifest entry.
Or include flag activity single top in your intent.
Instead of a new instance of your activity being created
every time you call start activity,
you get a call back to on new intent.
And users will be able to hit the Back button just once
to return to the previous activity.
The second case is around notifications.
We talked about how the Back button pops the back stack.
A natural corollary is that the Back button shouldn't
cross into different tasks-- a convention
since the days of Android 3.0.
It's just a single stack from where you are to the launcher.
Remember, there's that fancy overview screen
for switching tasks.
So for a notification that points
to an activity deep within your app,
you really don't want that first press of the Back button
to take you immediately to the launcher.
Not when every other time you're looking at that same screen,
the Back button does something different,
like go back in your app.
That's where TaskStackBuilder comes in.
It builds a synthetic, i.e.
fake, back stack.
By default, based on the parent activities
you attached to each activity entry in your manifest.
Although you can customize it any way
you want, adding extras, whatever you need.
This ensures that even if there is no task when the user taps
the notification, they're exactly where they
expect to be in your app.
As if they had navigated there themselves.
You just saved them some time and effort getting there.
So check out the blog post linked
in the description for all the details on tasks
and the back stack, plus some of the other flags
and launch modes you probably shouldn't use.
Well, right up until they're the perfect thing
to use to build better apps.
Thanks.
Wait, what was that?
The Up button?
Yeah, that's totally different.
Let's discuss in the blog post.
[MUSIC PLAYING]