Implementing Multiple Activities
All of the apps that we have seen so far in this book have had a single screen’s worth of UI. Many apps are somewhat more complicated than that, where we need to have lots of screens, such as:
- Showing a list of stuff
- Showing the details of a particular item out of that list
- Editing the details of a particular item, or adding a new one
- Settings to configure how the app behaves
- And so on
In the world of Jetpack, there are two main approaches for adding multiple screens. In this chapter, we will look at one: having more than one activity. In the next chapter, we will explore the other option: using fragments.
Multiple Activities, and Your App
There are at least two scenarios where your app may have to deal with multiple activities.
The first is the one outlined above: you want to have different screens for handling different bits of app functionality. In that case, you are the one writing the additional activities, and you will be the one to choose when those activities get displayed (e.g., bring up the details when the user taps on an item in the list). In this case, you have other options, such as the fragments mentioned above. But a lot of older Android projects will take the approach of having an activity for each distinct screen.
The second is when you have some piece of data that you want some other app to process. For example, you might have a URL to a Web page that you obtained from somewhere, and you want the user to view that Web page in their browser. In this case, you are not writing the Web browser (probably), but you are still deciding when that Web browser activity gets displayed. So, we need a way to start up another app, and that comes in the form of starting an activity from that app.
A less-common scenario is the inverse of the previous one. Suppose you are writing a Web browser. You want other apps to be able to hand you URLs, so you can display those Web pages to the user. In this case, you are writing the activity, but you may not be the one deciding when that activity gets displayed.
This chapter will focus on the first two of those scenarios, as they are the most common, though we will briefly cover how you support the third scenario.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.