JOANNA SMITH: This is your Recents menu,
also called the Overview screen.
Some apps have more than one card in this Overview screen,
which can make them seem special.
So how do those apps swing it?
Well, the Overview screen is not actually
a list of the recent apps a user has used,
despite what you may think.
Rather, it is a list of tasks.
Side note-- if you'd like to learn all about tasks,
check the link in the description below.
Ian has a lot to say.
Tasks are a great way for your app
to distinguish between disparate elements
where a user might want to switch between each piece.
Consider an email app with a task for the inbox
and a separate task for composing a new email, so
that the user can reference back to the existing email
when drafting their response.
This is a beautiful use of tasks.
Another useful way to think of how your app should use tasks
is to consider the multi-window implications.
In the multi-window world, each task will have its own window.
So if two elements of your app should logically
exist independently, but side by side,
that's a moment for tasks.
We call multiple tasks "concurrent documents."
This sense of your app multitasking
is where the "concurrent" part comes in.
You've got a stack of activities that the user probably
wants to interact with in parallel
with another stack of activities.
The "document" part is actually shorthand for anything
that you create that lives separately
from the rest of your app.
So it could actually be a document in a text editor,
but it could also be that draft email, or a separate browser
window, or a specific conversation
view for a messaging app.
So the first step in creating concurrent documents
is identifying what should logically exist independently.
And to that end, multi-window really
helps distinguish good cases for concurrent documents.
Any time you'd want to see two activities side by side,
those would have to be separate tasks.
To add a task to the Overview screen,
there is a pretty helpful manifest
attribute you can specify when registering
an activity in your manifest.
"android:documentLaunchMode" has four possible values.
"intoExisting" re-uses any existing alternative document
for this task, but creates a new task
if one doesn't already exist.
"always" will create a brand new document for the task.
"none" creates the task in the same window
as the current activity, a.k.a.
On the same stack.
This is the default behavior.
And "never" serves to override any specified flags
and permanently keep the task in the current stack.
What specified flags am I talking about?
Well, if you prefer to determine whether the task should
be in a separate document dynamically,
you can specify flags when starting the Intent.
To be clear, the creation of a task isn't actually special.
You'll create an Intent and pass it to "startActivity."
But the creation of that Intent is where you can get clever.
If you haven't already, it's time to meet the "addFlags()"
method.
"FLAG_ACTIVITY_NEW_DOCUMENT" is fairly straightforward.
It tells the system to treat this next task
as its own window in the Overview screen.
If there is an existing task with the same base component
name and data URI, the system will use it.
This means that your new document could be
replacing an existing document.
However, you can override this default behavior.
If you always want the system to create a brand new task,
you can add the "FLAG_ACTIVITY_MULTIPLE_TASK."
In multi-window mode, this gets even better,
as you can use "FLAG_ACTIVITY_LAUNCH_ADJACENT"
in conjunction with "FLAG_ACTIVITY_NEW_DOCUMENT"
to hint to the system that the task should be launched
alongside the existing task.
So the manifest attribute and the intent flags
offer you the same options.
"intoExisting" is equivalent to using
"FLAG_ACTIVITY_NEW_DOCUMENT," whereas, "always"
is equivalent to using "NEW_DOCUMENT" and
"MULTIPLE_TASKS" together.
Keep in mind, though, that an attribute only
needs to be specified once for the Activity,
while a flag will need to be added to every Intent.
So you'll probably want to choose
a path based on the variability of your Activity's use case.
But between manifest attributes for default document behavior
and flags for dynamic task creation,
your app will be dominating that Recent screen.
Just, you know, be sure that domination
is thoughtful and useful to a user,
because you don't want to go crazy
and get yourself uninstalled.
So now you're ready for concurrent documents.
Check out the blog post linked in the description for more
usage information and for code that you can copy and paste
into your own app to build better apps.
[MUSIC PLAYING]