CHAPTER 2:Development Environment 29 Note In addition to the actual Android devices,Android Studio also can run Android applications on an Android emulator that is running on the same development machine.To run the application on the Android emulator,simply select the Launch Emulator option in the Choose Device dialoa and then choose an android virtual device from the dropdown menu.Other Android emulator flavors also can be created using the AVD Manager button on the toolbar. Summary The Android provides a cmprehensive appli devel ers.In this chapter,you have learned how to successfully set up the Android development environment,including creating,building, and running an Android application on both the attached Android device and the Android emulator instance.The next chapter will go through the anatomy of an Android application
CHAPTER 2: Development Environment 29 Note In addition to the actual Android devices, Android Studio also can run Android applications on an Android emulator that is running on the same development machine. To run the application on the Android emulator, simply select the Launch Emulator option in the Choose Device dialog and then choose an Android virtual device from the dropdown menu. Other Android emulator flavors also can be created using the AVD Manager button on the toolbar. Summary The Android platform provides a comprehensive development toolchain for application developers. In this chapter, you have learned how to successfully set up the Android development environment, including creating, building, and running an Android application on both the attached Android device and the Android emulator instance. The next chapter will go through the anatomy of an Android application
Chapter Application Components The Android framework provides a set of components to enable the development of consistent and interoperable nobile applications.In this chapter Activity user can int lI screen on a limited display space,the name"activity"is given to an application window to reflect that it can only deliver a single and focused experience to the end user. On the Android platform,this approach of limiting the scope of every screen also makes it possible for applications to deliver a functionality by blending activities fro mother applications.Imagine in blending the ntact list activity intothe sending an e-mail.This makes it possible to code essand maintain unity of the entire platform by promoting reuse.You will learn more about communicating between applications in the section "Intents. Almost all activities require interaction with the user,and for that reason,the activity takes care of creating the window and laying out the user interface (Ul)components.You will learn more about the various Ul components available for your application in Chapter 4. 31
31 Chapter 3 Application Components The Android framework provides a set of components to enable the development of consistent and interoperable mobile applications. In this chapter you will learn about these fundamental components and what they provide. Activity “Activity” is the name given to a single application window with which the user can interact at any given time. As Android applications run full screen on a limited display space, the name “activity” is given to an application window to reflect that it can only deliver a single and focused experience to the end user. On the Android platform, this approach of limiting the scope of every screen also makes it possible for applications to deliver a functionality by blending activities from other applications. Imagine your e-mail client on your Android device seamlessly blending the contact list activity into the flow for sending an e-mail. This makes it possible to code less and maintain unity of the entire platform by promoting reuse. You will learn more about communicating between applications in the section “Intents.” Almost all activities require interaction with the user, and for that reason, the activity takes care of creating the window and laying out the user interface (UI) components. You will learn more about the various UI components available for your application in Chapter 4
CHAPTER 3:Application Components Creating an Activity You can create a new activity simply by deriving a new class from the android.app.Activity'class as shown in Listing 3-1. Listing 3-1.Creating a New Android Activity package com.apress.helloworld; import android.app.Activity; public class MyActivity extends Activity Declaring an Activity Deriving an Activity class does make the new activity available automatically.As activities can take different roles in an application,the Android platform requires some meta information about the new activity. This meta information needs to be provided as part of the Android manifest file through the <activity>2XML tag,as shown in Listing 3-2. Listing 3-2.Declaring the New Activity in the AndroidManifest.xml File <?xml version="1.0"encoding="utf-8"?> kmanifest xmlns:android-"http://schemas.android.com/apk/res/android" package="com.apress.helloworld"> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name android:theme="@style/AppTheme"> =".MyActivity" id:label-"@string/app_name"> tion.MAIN"/ category android:r e- http://developer.android.com/reference/android/app/Activity.html http://developer.android.com/guide/topics/manifest/activity-element.html
32 CHAPTER 3: Application Components Creating an Activity You can create a new activity simply by deriving a new class from the android.app.Activity1 class as shown in Listing 3-1. Listing 3-1. Creating a New Android Activity package com.apress.helloworld; import android.app.Activity; public class MyActivity extends Activity { } Declaring an Activity Deriving an Activity class does not make the new activity available automatically. As activities can take different roles in an application, the Android platform requires some meta information about the new activity. This meta information needs to be provided as part of the Android manifest file through the <activity>2 XML tag, as shown in Listing 3-2. Listing 3-2. Declaring the New Activity in the AndroidManifest.xml File <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.apress.helloworld" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MyActivity" android:label="@string/app_name" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= 1http://developer.android.com/reference/android/app/Activity.html. 2http://developer.android.com/guide/topics/manifest/activity-element.html
CHAPTER 3:Application Components 33 "android.intent.category.LAUNCHER"/> /intent-filter> </activity </application </manifest> As shown in Listing 3-2,the <activity>XML tag declares the new activity by providing its class name,its title,and how it should be exposed to the user Activity Life Cycle The life cycle of an activity is the group of states that an activity goes through from the time it is first created until it is destroyed. Caution The life cycle of the Android platform is much more complicated than the life cycle of ordinary desktop applications.The life cycle of desktop applications is directly under the user's control.On Android,the platform manages the life cycle of Android components in order to effectively use scarce system resources. out life cycle state change through a set of life cycle ho defined in the Activity class.Application developers can override those hooks to do appropriate work when the activity changes its state.Listing 3-3 shows these methods. Listing 3-3.Activity Life Cycle Hooks That Can Be Overridden public class MyActivity extends Activity{ protected void onCreate(Bundle savedInstanceState); protected void onStart(); protected void onRestart(): protected void onResume(); protected void onPause(); protected void onStop(); protected void onDestroy(); Figure 3-1 illustrates the order in which these hooks will get called by the Android platform as the activity goes through life cycle states
CHAPTER 3: Application Components 33 "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> As shown in Listing 3-2, the <activity> XML tag declares the new activity by providing its class name, its title, and how it should be exposed to the user. Activity Life Cycle The life cycle of an activity is the group of states that an activity goes through from the time it is first created until it is destroyed. Caution The life cycle of the Android platform is much more complicated than the life cycle of ordinary desktop applications. The life cycle of desktop applications is directly under the user’s control. On Android, the platform manages the life cycle of Android components in order to effectively use scarce system resources. An activity goes through seven life cycle states. The activity becomes informed about life cycle state changes through a set of life cycle hooks defined in the Activity class. Application developers can override those hooks to do appropriate work when the activity changes its state. Listing 3-3 shows these methods. Listing 3-3. Activity Life Cycle Hooks That Can Be Overridden public class MyActivity extends Activity { protected void onCreate(Bundle savedInstanceState); protected void onStart(); protected void onRestart(); protected void onResume(); protected void onPause(); protected void onStop(); protected void onDestroy(); } Figure 3-1 illustrates the order in which these hooks will get called by the Android platform as the activity goes through life cycle states
34 CHAPTER 3:Application Compor onRestart ble Aeair avigates back a m terminated the Activity to free resources Figure 3-1.Activity life cycle state machines Caution When overriding these life cycle hooks,make sure to invoke the super class's implementation first.Not doing that may cause random faulty behavior during application runtime
34 CHAPTER 3: Application Components Caution When overriding these life cycle hooks, make sure to invoke the super class’s implementation first. Not doing that may cause random faulty behavior during application runtime. Figure 3-1. Activity life cycle state machines