Understanding the Anatomy of an Android Application

Understanding the Anatomy of an Android Application

Mastering Important Android Terminology

This chapter introduces you to the terminology used in Android application development and provides you with a more thorough understanding of how Android applications function and interact with one another. Some of the important terms covered in this chapter are n Context: The context is the central command center for an Android application. All application-specific functionality can be accessed through the context. n Activity: An Android application is a collection of tasks, each of which is called an Activity. Each Activity within an application has a unique task or purpose. n Intent:The Android operating system uses an asynchronous messaging mechanism to match task requests with the appropriate Activity. Each request is packaged as an Intent.You can think of each such request as a message stating an intent to do something. n Service: Tasks that do not require user interaction can be encapsulated in a service.A service is most useful when the operations are lengthy (offloading time-consuming processing) or need to be done regularly (such as checking a server for new mail). Understanding the Anatomy of an Android Application 70 Chapter 4 Understanding the Anatomy of an Android Application Using the Application Context The application Context is the central location for all top-level application functionality. The Context class can be used to manage application-specific configuration details as well as application-wide operations and data. Use the application Context to access settings and resources shared across multiple Activity instances. Retrieving the Application Context You can retrieve the Context for the current process using the getApplicationContext() method, like this: Context context = getApplicationContext(); Using the Application Context After you have retrieved a valid application Context, it can be used to access applicationwide features and services. Retrieving Application Resources You can retrieve application resources using the getResources() method of the application Context.The most straightforward way to retrieve a resource is by using its resource identifier, a unique number automatically generated within the R.java class.The following example retrieves a String instance from the application resources by its resource ID: String greeting = getResources().getString(R.string.hello); We talk more about application resources in Chapter 6,“Managing Application Resources.” Accessing Application Preferences You can retrieve shared application preferences using the getSharedPreferences() method of the application Context.The SharedPreferences class can be used to save simple application data, such as configuration settings. We talk more about application preferences in Chapter 10,“Using Android Data and Storage APIs.” Accessing Other Application Functionality Using Context The application Context provides access to a number of other top-level application features. Here are a few more things you can do with the application Context: n Launch Activity instances n Retrieve assets packaged with the application n Request a system service (for example, location service) n Manage private application files, directories, and databases n Inspect and enforce application permissions 

Performing Application Tasks with Activities

The Android Activity class (android.app.Activity) is core to any Android application. Much of the time, you define and implement an Activity class for each screen in your application. For example, a simple game application might have the following five Activities, as shown in Figure 4.1: n A Startup or Splash screen: This activity serves as the primary entry point to the application. It displays the application name and version information and transitions to the Main menu after a short interval. n A Main Menu screen:This activity acts as a switch to drive the user to the core Activities of the application. Here the users must choose what they want to do within the application. n A Game Play screen: This activity is where the core game play occurs. n A High Scores screen: This activity might display game scores or settings. n A Help/About screen: This activity might display the information the user might need to play the game. Startup/Splash Activity Main Menu Activity Game Play Activity Help/About Activity High Scores Activity Figure 4.1 A simple game with five activities. The first item on this list—launching Activity instances—is perhaps the most common reason you use the application Context. 72 Chapter 4 Understanding the Anatomy of an Android Application The Lifecycle of an Android Activity Android applications can be multi-process, and the Android operating system allows multiple applications to run concurrently, provided memory and processing power is available. Applications can have background processes, and applications can be interrupted and paused when events such as phone calls occur.There can be only one active application visible to the user at a time—specifically, a single application Activity is in the foreground at any given time. The Android operating system keeps track of all Activity objects running by placing them on an Activity stack (see Figure 4.2).When a new Activity starts, the Activity on the top of the stack (the current foreground Activity) pauses, and the new Activity pushes onto the top of the stack.When that Activity finishes, that Activity is removed from the activity stack, and the previous Activity in the stack resumes. Android applications are responsible for managing their state and their memory, resources, and data.They must pause and resume seamlessly. Understanding the different states within the Activity lifecycle is the first step to designing and developing robust Android applications. Using Activity Callbacks to Manage Application State and Resources Different important state changes within the Activity lifecycle are punctuated by a series of important method callbacks.These callbacks are shown in Figure 4.3. Here are the method stubs for the most important callbacks of the Activity class: public class MyActivity extends Activity { protected void onCreate(Bundle savedInstanceState); protected void onStart(); protected void onRestart(); I am the top Activity. User can see and interact with me! I am the second Activity in the stack. If the user hits Back or the top Activity is destroyed, the user can see and interact with me again! I am an Activity in the middle of the stack. Users cannot see and interact with me until everyone above me is destroyed. I am an Activity at the bottom of the stack. If those Activities above me use too many resources, I will be destroyed! Figure 4.2 The Activity stack. Performing Application Tasks with Activities 73 onCreate() onStart() onRestart() onResume() Activity Sent to Background Activity Brought to Foreground Activity Brought to Foreground Activity Sent to Background Activity Running In Foreground Request Activity Start Activity Brought to Foreground Activity Killed for Memory onPause() onStop() onDestroy() Figure 4.3 The lifecycle of an Android Activity. Now let’s look at each of these callback methods, when they are called, and what they are used for. Initializing Static Activity Data in onCreate() When an Activity first starts, the onCreate() method is called.The onCreate() method has a single parameter, a Bundle, which is null if this is a newly started Activity. If this protected void onResume(); protected void onPause(); protected void onStop(); protected void onDestroy(); } 74 Chapter 4 Understanding the Anatomy of an Android Application Activity was killed for memory reasons and is now restarted, the Bundle contains the previous state information for this Activity so that it can reinitiate. It is appropriate to perform any setup, such as layout and data binding, in the onCreate() method.This includes calls to the setContentView() method. Initializing and Retrieving Activity Data in onResume() When the Activity reaches the top of the activity stack and becomes the foreground process, the onResume() method is called.Although the Activity might not be visible yet to the user, this is the most appropriate place to retrieve any instances to resources (exclusive or otherwise) that the Activity needs to run. Often, these resources are the most process-intensive, so we only keep these around while the Activity is in the foreground. Tip The onResume() method is the appropriate place to start audio, video, and animations. Stopping, Saving, and Releasing Activity Data in onPause() When another Activity moves to the top of the activity stack, the current Activity is informed that it is being pushed down the activity stack by way of the onPause() method. Here, the Activity should stop any audio, video, and animations it started in the onResume() method.This is also where you must deactivate resources such as database Cursor objects if you have opted to manage them manually, as opposed to having them managed automatically.

Formation et coursTélécharger le document complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *