Windows Phone Guide for Android Application Developers Table of Content


Windows Phone 8 Application LifeCycle and Tombstoning Example



Download 0.57 Mb.
Page12/15
Date04.05.2017
Size0.57 Mb.
#17233
1   ...   7   8   9   10   11   12   13   14   15

Windows Phone 8 Application LifeCycle and Tombstoning Example


Let us look at a simple example that illustrates the Windows Phone 8 state transitions and tombstoning. This is a one page shopping list application where the user can add items to the list. When the user leaves the application, the shopping list is saved automatically.

Saving the Application State


On Android, you may be saving application persistent state in a number of different ways such as SharedPreferences, using device internal storage or SQLite database..

To save the persistent state of the application on Windows Phone 8, i.e., the shopping list, we are going to use isolatedStorage. isolatedStorage is a sandboxed storage that is accessible only to that application. It ensures that one application cannot affect another application. To save the state, we first get the isolatedStore for the application. We then create a file in isolatedStorage, in which to save the persistent state.

To save the shopping list, we first serialize the list and then save it to the file.

public static void SaveShoppingList(ShoppingListInfo shoppingListInfo, string fileName)

{

//Get the isolatedStore for this application



using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())

{

// create a new file



using (IsolatedStorageFileStream fs = isf.CreateFile(fileName))

{

//and serialize data and save it



XmlSerializer xmlSer = new XmlSerializer(typeof(ShoppingListInfo));

xmlSer.Serialize(fs, shoppingListInfo);

}

}

}



In order to save transient state, Windows Phone 8 provides another class called PhoneApplicationService.State. We will see the use of this object below.

Application Launching


When the application is launched from the Start screen, the application received the Application_Launching event. This is equivalent to the activity onCreate, onStart and onResume methods being called.

To improve the application startup time, application should do as little work as possible and it should avoid loading application state from isolated storage or from remote servers.

In our application, we do no work and leave it to the appropriate page.

// Code to execute when the application is launching (eg, from Start)

// This code will not execute when the application is reactivated

private void Application_Launching(object sender, LaunchingEventArgs e)

{

}

Application Activation


When the user uses back button to navigate into the application, the application receives the Application_Activated event. Like the Launching event, doing resource intensive work in response to this event will delay the app from resuming. Just like Application_Launching, do as little work as possible.

// Code to execute when the application is activated (brought to foreground)

// This code will not execute when the application is first launched

private void Application_Activated(object sender, ActivatedEventArgs e)

{

if (e.IsApplicationInstancePreserved)

{

return;

}

// Check to see if the key for the application state data is in the State dictionary.

if (PhoneApplicationService.Current.State.ContainsKey("ApplicationDataObject"))

{

// If it exists, assign the data to the application member variable.

ApplicationDataObject = PhoneApplicationService.Current.State["ApplicationDataObject"] as ShoppingListInfo;

}

}

The IsApplicationInstancePreserved property of ActivatedEventArgs lets your app know if the app is returning from a dormant state or if it was tombstoned. Memory of the dormant application is preserved and as a result, we need not do anything.



Next we check if the application state is available in the state dictionary and load it in the AppliccationDataObject, a serialized string of the application state.


Application Deactivation


Let us examine what happens when the user navigates forward from the application.e.g. by hitting the start button while running the application. When the user hits the home button on Android the onResume of the activity is called. On Windows Phone 8, the application will receive the Application_Deactivated event. The application would be made dormant and moreover, it may also be tombstoned. As a result, we save the application persistent state both in the application state dictionary as well as in isolateStorage. If this application is reactivated, we will restore either from the state dictionary or from the isolatedStorage.

// Code to execute when the application is deactivated (sent to background)

// This code will not execute when the application is closing

private void Application_Deactivated(object sender, DeactivatedEventArgs e)

{

// If there is data in the application member variable...

if (ApplicationDataObject != null)

{

// Store it in the State dictionary.

PhoneApplicationService.Current.State["ApplicationDataObject"] = ApplicationDataObject;
// Also store it in isolated storage, in case the application is never reactivated.

SaveShoppingList(ApplicationDataObject, "myDataFile.txt");

}

}

Application Closing


This event is raised when the user uses the back button to navigate backward past the first page, after which the app is terminated. The user must re-launch the application to use it. As a result, you should save any persistent data but not the transient data.

// Code to execute when the application is closing (eg, user hit Back)

// This code will not execute when the application is deactivated

private void Application_Closing(object sender, ClosingEventArgs e)

{

if (ApplicationDataObject != null)

{

// Also store it in isolated storage, so that we can restore the shopping list

SaveShoppingList(ApplicationDataObject, "myDataFile.txt");

}

}

This event is comparable to the onDestroy method of the activity.




Page Operations


In addition to the above application events, each page needs to override certain methods to implement Fast Application Switching.

OnNavigatedFrom


OnNavigatedFrom method is called whenever the user navigates away from the page, i.e. forward or back. In our method implementation, we determine if the user navigated forward, we save the transient state of the page. This transient state is stored in the application state dictionary. And we save the rest of the page state in the applications data object which will be saved to isolatedStorage if the user leaves the application. The transient state is used if the navigates back to the same page using back button.

protected override void OnNavigatedFrom(NavigationEventArgs e)

{

if (e.NavigationMode !=

System.Windows.Navigation.NavigationMode.Back)

{

PhoneApplicationService.Current.State["ShoppingListItem"] = txtItem.Text;

}

(Application.Current as ShoppingList.App).ApplicationDataObject.ItemsList = shoppingItems;

}

OnNavigatedTo


OnNavigatedTo method is called whenever the user navigates to a page. In this method, we check _isNewPageInstance to see if this is a new instance of the page or if the user is navigating to a page that is already in memory. If it is a new page, and we have restored application state, we load the page UI from the application state. Otherwise, we load the page by using a method called GetDataAsync. This method loads the application state asynchronously so that the page is responsive to user as we load it.

Once we have loaded the persistent state of the page, we load the transient data if any data is stored in the application state. If any transient state is available in the state dictionary, we load it and populate the page controls.



protected override void OnNavigatedTo(NavigationEventArgs e)

{

// If _isNewPageInstance is true, the page constructor has been called, so

// state may need to be restored.

if (_isNewPageInstance)

{

// If the application member variable is not empty,

// set the page's data object from the application member variable.

if ((Application.Current as ShoppingList.App).ApplicationDataObject != null)

{

UpdateApplicationDataUI();

}

else

{

// Otherwise, call the method that loads data.

(Application.Current as ShoppingList.App).GetDataAsync();

}

}

if (PhoneApplicationService.Current.State.ContainsKey("ShoppingListItem"))

{

// If it exists, assign the data to the application member variable.

txtItem.Text = PhoneApplicationService.Current.State["ShoppingListItem"] as string;

}

// Set _isNewPageInstance to false. If the user navigates back to this page

// and it has remained in memory, this value will continue to be false.

_isNewPageInstance = false;

}




Download 0.57 Mb.

Share with your friends:
1   ...   7   8   9   10   11   12   13   14   15




The database is protected by copyright ©ininet.org 2024
send message

    Main page