Tasks and the Back Stack

Android uses tasks to keep track of the 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 the stack. If you are looking at the base activity and press 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 23.4). 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 23.4  CriminalIntent task

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 23.5).

Figure 23.5  Pressing the Back button in CriminalIntent

Pressing the Back button in CriminalIntent

Switching between tasks

Using the overview screen, you can switch between tasks without affecting each task’s state. 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 switching tasks using 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. If you skipped over the CriminalIntent chapters and did not build the app, you can access the code in the solutions file at www.bignerdranch.com/​solutions/​AndroidProgramming4e.zip.)

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). Finally, open the overview screen using the Recents button (next to the Home button) (Figure 23.6).

Figure 23.6  Overview screen on Nougat (left) and Pie (right)

Overview screen on Nougat (left) and Pie (right)

The overview screen on the left in Figure 23.6 is what users will see if they are running Android Nougat (API level 24). The overview screen on the right is what users running Android Pie (API level 28) will see (so long as they do not have the Swipe up on Home option enabled, as discussed in Chapter 3).

In both cases, the entry displayed for each app (known as a card) represents the app’s task. A screenshot of the activity at the top of each task’s back stack is displayed. You can press on the BeatBox or CriminalIntent card to return to the app (and to whatever activity you were interacting with in that app).

You 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

When you start an activity, sometimes 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 23.7.

Figure 23.7  NerdLauncher’s task contains CriminalIntent

NerdLauncher’s task contains 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 only see one task in the overview screen even though you started a different app.

When CriminalIntent’s MainActivity was started, it was added to NerdLauncher’s task (Figure 23.8). If you press the NerdLauncher task, you will be returned to whatever CriminalIntent screen you were looking at before starting the overview screen.

Figure 23.8  CriminalIntent not in its own task

CriminalIntent not in its own task

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

Figure 23.9  Launching CriminalIntent into its own task

Launching CriminalIntent into its own task

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

Listing 23.9  Adding a new task flag to the intent (NerdLauncherActivity.kt)

class NerdLauncherActivity : AppCompatActivity() {
    ...
    private class ActivityHolder(itemView: View) :
            RecyclerView.ViewHolder(itemView),
            View.OnClickListener {
        ...
        override fun onClick(view: View) {
            val activityInfo = resolveInfo.activityInfo

            val intent = Intent(Intent.ACTION_MAIN).apply {
                setClassName(activityInfo.applicationInfo.packageName,
                    activityInfo.name)
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }

            val context = view.context
            context.startActivity(intent)
        }
    }
    ...
}

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 23.10).

Figure 23.10  CriminalIntent now in its own task

CriminalIntent now in its own task

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. MainActivity 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
52.14.8.34