In Android, the building blocks of applications are activities, so before you start banging out bits and bytes, you need a firm understanding of what activities are and how they work. An activity is a single, focused thing a user can do or interact with in an application. For example, when an app presents a list of menu items the user can choose from, that's an activity. When an app displays photographs along with their captions, that's also an activity. An application may consist of just one activity, or like most applications in the Android system, it may contain several. Though activities may work together to appear to be one cohesive application, they are actually independent of each other. An activity in Android is an important part of an application's overall life cycle, and the way the activities are launched and put together is a fundamental part of the Android's application model.
In Android, each activity is implemented as an implementation of the Activity base class. Almost all activities interact with the user, so the Activity class takes care of creating the window for you in which you can place your user interface (UI). Activities are most often presented in full-screen mode, but in some instances, you can find activities floating in windows or embedded inside other activities — this is known as an activity group.
Activities in the system are managed as an activity stack. As users work with a tablet, they move from activity to activity, sometimes across applications. When a new activity is created, it is placed on top of the stack and becomes the running activity. The previous running activity always remains below it in the stack and does not come to the foreground again until the new activity exits.
I cannot stress enough the importance of understanding how and why the activity works behind the scenes. Not only will you come away with a better understanding of the Android platform, but you will also be able to accurately troubleshoot why your application is doing very odd things at run time.
An activity has essentially four states, as shown in Table 7-1.
Activity State | Description |
Active/running | The activity is in the foreground of the screen (at the top of the stack). |
Paused | The activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity). A paused activity is completely alive, meaning that it can completely maintain state and member information and remains attached to the window manager that controls the windows in Android. However, note that the activity can be killed by the Android system in extreme low-memory conditions. |
Stopped | If an activity becomes completely obscured by another activity, it is stopped. It retains all state and member information, but it is not visible to the user. Therefore, the window is hidden and will often be killed by the Android system when memory is needed elsewhere. |
Create and resuming | The system has either paused or stopped the activity. The system can either reclaim the memory by asking it to finish or it can kill the process. When it displays that activity to the user, it must resume by restarting and restoring to its previous state. |
Pictures are worth a thousand words, and, in my opinion, flow diagrams are worth ten times that. The following diagram (Figure 7-1) shows the important paths of an activity. This is the activity life cycle.
The rectangles represent callback methods you can implement to respond to events in the activity. The shaded ovals are the major states that the activity can be in.
Take note of these three loops in your activity:
The activity life cycle is a large and complex topic. I give you the basics here so that you understand the applications you will be building in this book, but I highly advise that you read through the Activity Life Cycle and Process Life Cycle portions of the Android documentation at http://d.android.com/reference/android/app/Activity.html#ActivityLifecycle and http://d.android.com/reference/android/app/Activity.html#ProcessLifecycle.
If you are interested in breaking apart your application activities into different manageable chunks, then Fragments may interest you. Fragments were introduced in Android 3.0 to allow you to build more sophisticated and modularized layouts. Fragments are a beast all unto their own and a full discussion of them requires more space than I have in this book. If you are interested, please see the Fragment documentation online at http://d.android.com/guide/topics/fundamentals/fragments.html.
The entire activity life cycle boils down to the following methods. All methods can be overridden, and custom code can be placed in all of them. All activities implement onCreate() for initialization and may also implement onPause() for cleanup. You should always call the superclass (base class) when implementing these methods:
public class Activity extends ApplicationContext { protected void onCreate(Bundle savedInstanceState); protected void onStart(); protected void onRestart(); protected void onResume(); protected void onPause(); protected void onStop(); protected void onDestroy(); }
In general, the movement an activity makes through its life cycle looks like this:
The methods marked as killable can be “killed” by the Android system at any time without notice. Because of this, you should set up the onPause() method to do any last cleanup and to write any persistent data (such as user edits to data) to your storage mechanism.
Configuration changes are important because they can affect an activity's life cycle. A configuration change is defined as a change in screen orientation (the user moving the screen to the side and then back, portrait to landscape and vice versa), language, input devices, and so on. When a configuration change happens, your activity's resources, layout files, and so on might need to change in order to work properly. For example, an application may look completely different if it is interacting with the user in portrait mode as compared to landscape mode (on its side). To deal with this, when a configuration change occurs, your activity is destroyed and goes through the normal activity end-of-life cycle of onPause()onStop()onDestroy(). After the onDestroy() method is called, the system creates a new instance of the activity, one that uses the new system configuration.
3.144.82.154