Tasks and the Back Stack

Android uses tasks to keep track of the user’s state within each running application. Each application opened from Android’s default launcher app gets its own task. This is the desired behavior but, unfortunately for your NerdLauncher, it is not the default behavior. Before you foray into forcing applications to launch into their own tasks, let’s discuss what tasks are and how they work.

A task is a stack of activities that the user is concerned with. The activity at the bottom of the stack is called the base activity, and whatever activity is on top is the activity that the user sees. When you press the Back button, you are popping the top activity off of this stack. If you are looking at the base activity and hit the Back button, it will send you to the Home screen.

By default, new activities are started in the current task. In CriminalIntent, whenever you started a new activity, that activity was added to the current task (as shown in Figure 24.5). This was true even if the activity was not part of the CriminalIntent application, like when you started an activity to select a crime suspect.

Figure 24.5  CriminalIntent task

Figure shows CriminalIntent task.

The benefit of adding an activity to the current task is that the user can navigate back through the task instead of the application hierarchy (as shown in Figure 24.6).

Figure 24.6  Pressing the Back button in CriminalIntent

Figure shows how Pressing the Back button in CriminalIntent works.

Switching between tasks

Using the overview screen, you can switch between tasks without affecting each task’s state, as you first saw way back in Chapter 3. For instance, if you start entering a new contact and switch to checking your Twitter feed, you will have two tasks started. If you switch back to editing contacts, your place in both tasks will be saved.

Try out the overview screen on your device or emulator. First, launch CriminalIntent from the Home screen or from your app launcher. (If your device or emulator no longer has CriminalIntent installed, open your CriminalIntent project in Android Studio and run it from there.) Select a crime from the crime list. Then push the Home button to return to the Home screen. Next, launch BeatBox from the Home screen or from your app launcher (or, if necessary, from Android Studio).

Open the overview screen by pressing the Recents button (Figure 24.7).

Figure 24.7  Overview screen versions

Set of two screenshots are shown.

The overview screen displayed on the left in Figure 24.7 is what users will see if they are running KitKat. The overview screen displayed on the right is what users running Lollipop or later will see. In both cases, the entry displayed for each app (known as a card from Lollipop forward) represents the task for each app. A screenshot of the activity at the top of each task’s back stack is displayed. Users can press on the BeatBox or CriminalIntent entry to return to the app (and to whatever activity they were interacting with in that app).

Users can clear an app’s task by swiping on the card entry to remove the card from the task list. Clearing the task removes all activities from the application’s back stack.

Try clearing CriminalIntent’s task, then relaunching the app. You will see the list of crimes instead of the crime you were editing before you cleared the task.

Starting a new task

Sometimes, when you start an activity, you want the activity added to the current task. Other times, you want it started in a new task that is independent of the activity that started it.

Right now, any activity started from NerdLauncher is added to NerdLauncher’s task, as depicted in Figure 24.8.

Figure 24.8  NerdLauncher’s task contains CriminalIntent

Figure shows NerdLauncher’s task containing CriminalIntent.

You can confirm this by clearing all the tasks displayed in the overview screen. Then start NerdLauncher and click on the CriminalIntent entry to launch the CriminalIntent app. Open the overview screen again. You will not see CriminalIntent listed anywhere. When CrimeListActivity was started, it was added to NerdLauncher’s task (Figure 24.9). If you press the NerdLauncher task, you will be returned to whatever CriminalIntent screen you were looking at before starting the overview screen.

Figure 24.9  CriminalIntent not in its own task

Screenshot shows NerdLauncher app in Android phone. The NerdLauncher app shows a crime record reading, Somebody stole my yogurt.

This will not do. Instead, you want NerdLauncher to start activities in new tasks (Figure 24.10). That way each application opened by pressing an item in the NerdLauncher list gets its own task, which will allow users to switch between running applications via the overview screen, NerdLauncher, or the Home screen, as they prefer.

Figure 24.10  Launching CriminalIntent into its own task

Figure shows Launching CriminalIntent into its own task.

To start a new task when you start a new activity, add a flag to the intent in NerdLauncherFragment.java.

Listing 24.9  Adding new task flag to intent (NerdLauncherFragment.java)

public class NerdLauncherFragment extends Fragment {
    ...
    private class ActivityHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {
        ...
        @Override
        public void onClick(View v) {
            ...
            Intent i = new Intent(Intent.ACTION_MAIN)
                    .setClassName(activityInfo.applicationInfo.packageName,
                            activityInfo.name)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(i);
        }
    }
    ...
}

Clear the tasks listed in your overview screen. Run NerdLauncher and start CriminalIntent. This time, when you pull up the overview screen you will see a separate task for CriminalIntent (Figure 24.11).

Figure 24.11  CriminalIntent now in its own task

Screenshot shows two separate tasks. The NerdLauncher app screen and CriminalIntent app screens are shown in Overview.

If you start CriminalIntent from NerdLauncher again, you will not create a second CriminalIntent task. The FLAG_ACTIVITY_NEW_TASK flag by itself creates one task per activity. CrimeListActivity already has a task running, so Android will switch to that task instead of starting a new one.

Try this out. Open the detail screen for one of the crimes in CriminalIntent. Use the overview screen to switch to NerdLauncher. Press on CriminalIntent in the list. You will be right back where you were in the CriminalIntent app, viewing the details for a single crime.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.237.91.98