As the application makes state transitions in response to various actions or events, Android makes callbacks on the application. In response, the application has to save appropriate state or load it depending on the event. Similarly, Windows Phone OS raises different events in response to which the app has to save or load app state.
In order to support Android multitasking, Android application developers have certain responsibilities. When the application is moved to the background, they need to save the application state, which must then restored if the activity is recreated. If the activity is resumed or restarted, the state is intact and there is no need to recreate the state.
In particular, the application moving to background will receive a callback, onPause(), at which time the application should commit unsaved changes to persistent state and reduce any CPU intensive tasks such as animations. This processing should be quick since another activity is not started till onPause of the previous activity completes. When the activity is no longer visible to the user, onStop() is called at which time, the activity should stop all UI operations. When the system destroys the activity to reclaim memory, onDestroy() is called. When activity is created for the first time, the activity should restore previously frozen state. When the activity becomes visible to the user, system called onStart() method whereas when it comes to foreground, the system calls onResume().
Windows Phone Application LifeCycle
The following state diagram shows the various states and explains what the developer should do in response to various events.
See App activation and deactivation for Windows Phone for details of application states and transitions.
Comparing Life-cycle Methods
We can compare the Android lifecycle method with Windows Phone 8’s state-change events and Page methods. Note that these mappings are approximate since Android provides different ways to save and restore state.
Android Methods
|
Windows Phone Application Events
|
Windows Phone Page Methods
|
onCreate()
|
Application_Launching
|
|
onStart(),
|
|
|
onResume()
|
|
NavigatedTo
|
onPause(),onSaveInstanceState
|
|
NavigatedFrom
|
onStop(), onSaveInstanceState
|
Application_Deactivated
|
|
onRestart()
|
Application_Activated
|
|
onDestroy()
|
Application_Closing
|
|
In Android, you can use the onCreate() method to initialize the activity, to setup or recreate the activity state. In Windows Phone 8, application initialization is performed in response to application_launching event. Windows Phone application should also load any persistent state, state that should be preserved between instances of the application, at that time.
In Android, if a user launches a new application, the application in the foreground receives onPause and onStop callbacks. You need to commit the application data to the persistent storage or database as part of onPause (or during onSaveInstanceState). In Windows Phone 8, if a user launches a new application, the current foreground application is deactivated. At the time of deactivation, the application receives the Application_Deactivated event and you need to save the application’s state in its event handler. You have 10 seconds to complete the processing.
When application is resume, Android makes onRestart and onResume callback methods. If the activity was in background, there is no need to restore state, . However, if the activity was destroyed, persistent state must be restored. In Windows Phone 8, when a user starts a dormant or tombstoned application, the Application_activated event is raised. If the application is reactivated from a dormant state as indicated by IsApplicationInstancePreserved property, the application need not do anything. Otherwise, you should restore the application state from the State object where the state is saved during deactivation.
In Android, when one Activity replaces another, the current Activity receives the onPause and onStop callbacks. This behavior is similar to the Page to Page navigation in Windows Phone 8. While moving from one page to another, the current page receives the NavigatedFrom() event and the new page receives the NavigatedTo() event.
Share with your friends: |