CHAPTER 3:Application Components 35 onCreate is called when the activity gets created.All activities implement this method in order to initialize the activity and its Ul.This method also takes an android. os.Bundle3 object that may contain the frozen state from the previous run of this activity.At this stage the activity is not yet visible to the user. Note A bundle is used to carry data between activities,services,and even applications on Android.It is an Android construct that holds plain data as a map of key/value pairs.The stored data is serialized into a byte stream by the platform and later is deserialized into proper objects when restored.This type of data is known as a Parcelon the Android platform. onStart is called when the activity becomes visible to still cannot inte onResume is called when the activity is foregrounded. At this state,the user can interact with the application. onPause is called when the activity is no longer in the foreground.As the user may not come back,the activity is expected to save its current state into a bundle. tagsca6dwhentheacntwsnolongrebeo onRestart is called if the activity becomes visible again. The onStart method gets called next. onDestroy is called when the platform is destroying the activity. Caution After the onStop or onDestroy methods return,the platform may decide to kill the application process at any time.Activities are expected to save their current state during the call to the onPause method. shttp://developer.android.com/reference/android/os/Bundle.html. http://developer.android.com/reference/android/os/Parcel.html
CHAPTER 3: Application Components 35 Note A bundle is used to carry data between activities, services, and even applications on Android. It is an Android construct that holds plain data as a map of key/value pairs. The stored data is serialized into a byte stream by the platform and later is deserialized into proper objects when restored. This type of data is known as a Parcel4 on the Android platform. onCreate is called when the activity gets created. All activities implement this method in order to initialize the activity and its UI. This method also takes an android. os.Bundle3 object that may contain the frozen state from the previous run of this activity. At this stage the activity is not yet visible to the user. onStart is called when the activity becomes visible to the user. Although the activity is now visible, the user still cannot interact with it until the application is foregrounded. onResume is called when the activity is foregrounded. At this state, the user can interact with the application. onPause is called when the activity is no longer in the foreground. As the user may not come back, the activity is expected to save its current state into a bundle. onStop is called when the activity is no longer visible to the user. onRestart is called if the activity becomes visible again. The onStart method gets called next. onDestroy is called when the platform is destroying the activity. 3http://developer.android.com/reference/android/os/Bundle.html. 4http://developer.android.com/reference/android/os/Parcel.html. Caution After the onStop or onDestroy methods return, the platform may decide to kill the application process at any time. Activities are expected to save their current state during the call to the onPause method
CHAPTER 3:Application Components Intent As mentioned earlier in the Activity section,the Android platform is designed to be highly promotin ons that are pre nt on the om each other to provide a certain functionality to the user.This is achieved through a late runtime binding facility,known as android.content.Intent,s which the Android framework provides. The intent holds a passive data structure that holds an abstract description of an action to be performed.The primary pieces of information in an intent are action,which is the general action to be performed such as ACTION_VIEW,ACTION_EDIT. data,which is a URI(uniform resource identifier) obiect that refers to the data to be acted on.such as content://contacts/people/1,tel:6501231234. The optional information pieces in an intent are type,which is the MIME type for the data. component,which explicitly names the component,such as a specific activity,that should handle the action. extras,which are a bundle that carries additional information for the action as a key/value pair. The most significant use of an intent is to launch activities and services Listing 3-4 demonstrates how you can launch the contact list activity to enable the user to browse through the contacts. Listing 3-4.Code to Launch the Contact List Through an Intent Intent intent new Intent(): intent.setAction(Intent.ACTION VIEW); intent.setData(Uri.parse("content://contacts/people/")); startActivity(intent); shttp://developer.android.com/reference/android/content/Intent.html
36 CHAPTER 3: Application Components Intent As mentioned earlier in the Activity section, the Android platform is designed to be highly modular, promoting collaboration among the applications that are present on the device. Applications can blend activities from each other to provide a certain functionality to the user. This is achieved through a late runtime binding facility, known as android.content.Intent, 5 which the Android framework provides. The intent holds a passive data structure that holds an abstract description of an action to be performed. The primary pieces of information in an intent are action, which is the general action to be performed, such as ACTION_VIEW, ACTION_EDIT. data, which is a URI (uniform resource identifier) object that refers to the data to be acted on, such as content://contacts/people/1, tel:6501231234. The optional information pieces in an intent are type, which is the MIME type for the data. component, which explicitly names the component, such as a specific activity, that should handle the action. extras, which are a bundle that carries additional information for the action as a key/value pair. The most significant use of an intent is to launch activities and services. Listing 3-4 demonstrates how you can launch the contact list activity to enable the user to browse through the contacts. Listing 3-4. Code to Launch the Contact List Through an Intent Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("content://contacts/people/")); startActivity(intent); 5http://developer.android.com/reference/android/content/Intent.html
CHAPTER 3:Application Components 37 Intent Resolution Once the intent is created and dispatched through the Android framework,the Android platform resolves the intent to find candidates that can provide the requested action.There are two main groups of intents that define how they get resolved and dispatched. Explicit intents are intents with an explicitly specified aticall component Listing3-5.Intent with an Explicitly Specified Componen Intent intent-new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setComponent(new ComponentName( com.apress 'com.apress.MyActivity")); startActivity(intent); ■ comp n the information prov e n the intent,the Android platform goes through all of the installed application's intent filters to come up with the best matches that can fulfill the requested action. Intent Filters Intent filters allow the application to declare the list of intents that it can red in the application's manifest file,AndroidManifest.xm Going back to Listing 3-2,you will recall that MyActivity declared the following intent filter: <activity -filter estring/app_name"> id.intent.action.MAIN"/> </intent-filter> </activity>
CHAPTER 3: Application Components 37 Intent Resolution Once the intent is created and dispatched through the Android framework, the Android platform resolves the intent to find candidates that can provide the requested action. There are two main groups of intents that define how they get resolved and dispatched. Explicit intents are intents with an explicitly specified component, as shown in Listing 3-5. These intents automatically get dispatched to the specified component. Listing 3-5. Intent with an Explicitly Specified Component Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setComponent(new ComponentName( "com.apress", "com.apress.MyActivity")); startActivity(intent); Implicit intents are intents that do not specify a component. Based on the information provided in the intent, the Android platform goes through all of the installed application's intent filters to come up with the best matches that can fulfill the requested action. Intent Filters Intent filters allow the application to declare the list of intents that it can fulfill. It is declared in the application’s manifest file, AndroidManifest.xml. Going back to Listing 3-2, you will recall that MyActivity declared the following intent filter: <activity android:name=".MyActivity" android:label="@string/app_name" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity>
CHAPTER 3:Application Components This intent filter instructs the Android platform that this activity should be included in the resolution of Launcher activities.This way,the icon of the new application gets shown on the Launcher screen for the user to be able to start it. Multiple intent filters can be declared for a single activity as well.For example,if this activity can fulfll a request to play a video file,it can declare an intent filter similar to the following: <activity saction andro "android.intent.action.MAIN"/> android.intent.category.LAUNCHER"/> </intent-filter> <intent-filter> <action android:name= "android.intent.action.VIEW"/> <category android:name= 'android.intent.category.DEFAULT"/> ata andre id:mimeType="video/*"/> ilter </activity> As indicated earlier,intents are not only used to launch activities.In this chapter you will learn how to use intents to interact with services and broadcast messages.Intent filters can also be created through code using the android.content.IntentFilters class;you will see an example in the section"Broadcast Messages. Getting and Extracting the Intent Once the intent is properly dispatched and the activity gets started,the newly started activity gets hold of the intent by using the getIntent() method of the Activity class,as shown in Listing 3-6. ehttp://developer.android.com/reference/android/content/IntentFilter.html
38 CHAPTER 3: Application Components This intent filter instructs the Android platform that this activity should be included in the resolution of Launcher activities. This way, the icon of the new application gets shown on the Launcher screen for the user to be able to start it. Multiple intent filters can be declared for a single activity as well. For example, if this activity can fulfill a request to play a video file, it can declare an intent filter similar to the following: <activity android:name=".MyActivity" android:label="@string/app_name" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= "android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name= "android.intent.action.VIEW" /> <category android:name= "android.intent.category.DEFAULT" /> <data android:mimeType="video/*" /> </intent-filter> </activity> As indicated earlier, intents are not only used to launch activities. In this chapter you will learn how to use intents to interact with services and broadcast messages. Intent filters can also be created through code using the android.content.IntentFilter6 class; you will see an example in the section “Broadcast Messages.” Getting and Extracting the Intent Once the intent is properly dispatched and the activity gets started, the newly started activity gets hold of the intent by using the getIntent() method of the Activity class, as shown in Listing 3-6. 6http://developer.android.com/reference/android/content/IntentFilter.html
CHAPTER 3:Application Components 39 Listing 3-6.Accessing the Intent That Started the Activity Intent intent getIntent(); Uri data-intent.getData(); else if (inte /edit action etAction().equals(Intent.ACTION_EDIT)){ Pending Intent Although intents are mostly used to start an operion,they are also used for registering callbacks Note A"callback"in computer programming refers to a reference to a function that is passed as an argument to another code,which is expected to call back the originating code by executing the referenced code at some time in the future.It is mostly used to sign up to be notified for events. An application can provide an intent to another application for it to call the application back when a certain condition is met,or a requested operation completed.For example,when an application places a notification on the n63 it also provides an t to th cation bar to call the application back when the user clicks that notification. Ordinary intents cannot be used directly for this purpose,as they are executed based on the calling application's permissions.The Android framework provides a special intent type called android.app.PendingIntent.?This special intent type is executed based on the creating application's permissions instead of on the executing application's permissions.This makes it possible for applications to use pending intents as callback intents. http://developer.android.com/reference/android/app/PendingIntent.html. www.allitebooks.com
CHAPTER 3: Application Components 39 Listing 3-6. Accessing the Intent That Started the Activity Intent intent = getIntent(); Uri data = intent.getData(); if (intent.getAction().equals(Intent.ACTION_VIEW)) { // view action } else if (intent.getAction().equals(Intent.ACTION_EDIT)) { // edit action } Pending Intent Although intents are mostly used to start an operation, they are also used for registering callbacks. Note A “callback” in computer programming refers to a reference to a function that is passed as an argument to another code, which is expected to call back the originating code by executing the referenced code at some time in the future. It is mostly used to sign up to be notified for events. An application can provide an intent to another application for it to call the application back when a certain condition is met, or a requested operation is completed. For example, when an application places a notification on the notification bar, it also provides an intent to the notification bar to call the application back when the user clicks that notification. Ordinary intents cannot be used directly for this purpose, as they are executed based on the calling application’s permissions. The Android framework provides a special intent type called android.app.PendingIntent. 7 This special intent type is executed based on the creating application’s permissions instead of on the executing application’s permissions. This makes it possible for applications to use pending intents as callback intents. 7http://developer.android.com/reference/android/app/PendingIntent.html. www.allitebooks.com