Blog

All Blog Posts

WP7 Insights #3: Storing User State - Activating and Deactivating

This article is the third in a series which discusses some of the things that I learned while developing my first Windows Phone 7 application.

The Dwolla app contains a login screen which requires a user to enter their Dwolla credentials in order to view their account. One common task in many web application login screens is to include a checkbox to allow the user to "Remember Me," so that users don't have to rekey their username on every trip to the login page.

To accomplish this in my Windows Phone 7 application, I used two concepts: Here is the workflow of how this all will work.
  • User enters their username and password on the login screen and checks the Remember Me checkbox.
  • User does some stuff - hopefully send me some money :)
  • User exits the application (via clicking on the Windows button, logout, etc).
  • The application fires either the Closing or Deactivated events.  These events call a method to Save Settings.
  • The user navigates back to the Dwolla app.
  • The application fires either the Launching or Activated events.  These events call a method to Load Settings.
  • The login page is displayed.  The page uses a value set by the Load Settings method to prefill the username field.
Wiring up the events To start I added the following code to the App.xaml file in my project to wire up events in the PhoneApplicationService class.

 <Application.ApplicationLifetimeObjects>
     <!--Required object that handles lifetime events for the application-->
     <shell:PhoneApplicationService
         Launching="Application_Launching" Closing="Application_Closing"
         Activated="Application_Activated" Deactivated="Application_Deactivated"/>
 </Application.ApplicationLifetimeObjects>
Here is the code in the App.xaml.cs class for these events:
     private void Application_Launching(object sender, LaunchingEventArgs e)
     {
         LoadSettings();
     }
     private void Application_Activated(object sender, ActivatedEventArgs e)
     {
         LoadSettings();
         // Ensure that application state is restored appropriately
         if (!App.ViewModel.IsDataLoaded)
         {
App.ViewModel.LoadData();
         }
     }
     private void Application_Deactivated(object sender, DeactivatedEventArgs e)
     {
         SaveSettings();
     }
     private void Application_Closing(object sender, ClosingEventArgs e)
     {
         // Ensure that required application state is persisted here.
         SaveSettings();
     }

 The following code shows how I save the settings for the user. Note that I created a class, LoginInfo, to store all of my settings. It worked well for me to create one class to store my Login Information rather than have a bunch of individual settings. I also have a class called Globals which contains UserLoginInfo, an instance of LoginInfo. Globals.cs

   public static LoginInfo UserLoginInfo = new LoginInfo();
App.xaml.cs
     void SaveSettings()
     {
         IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
         if (Globals.UserLoginInfo != null)
         {
settings["UserLoginInfo"] = Globals.UserLoginInfo;
settings.Save();
         }
     }

 Here is the code to load the settings previously saved to Isolated Storage.  I am going to take the values and load them into my UserLoginInfo object in my Globals class.  There are other ways to store data on WP7 (e.g. ViewModel, using the App class), but I find this technique has worked well for me in both Silverlight and WP7. App.xaml.cs

     void LoadSettings()
     {
         IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
         if (settings.Contains("UserLoginInfo"))
         {
Globals.UserLoginInfo = (LoginInfo)settings["UserLoginInfo"];
         }
     }

Finally in my login page I interrogate the UserLoginInfo class to determine if the Remember Me box was last checked and if so, what the username should be.  Here is the code snippet for this.

if (Globals.UserLoginInfo != null)
    chkRememberMe.IsChecked = Globals.UserLoginInfo.RememberMe;
if (Globals.UserLoginInfo != null && Globals.UserLoginInfo.RememberMe)
{
    txtUserName.Text = Globals.UserLoginInfo.EmailAddress;
}

 That's it!  Now users returning to the Dwolla app will have their username prefilled from their previous session and save them time so they can send money quicker.  I hope you find these tips useful.  Thanks! Resources