Note : This post has not adhered to the  best practice to acheive  Context Switching….. This is not best practice

Very first let us understand what I mean by word “Context Switching “here?

Context switching in context of this post is essentially, navigate back to the page from where application got deactivated.

image

There are three steps we need to do to perform above task  Smile

  1. Find the URL of current page while deactivating the application
  2. Save the URL in isolated storage while deactivating the application
  3. At application activating or launching set the start page of application dynamically by reading URL of the page from isolated state.

Define isolated storage setting globally in app.xaml


public IsolatedStorageSettings isoStorage = IsolatedStorageSettings.ApplicationSettings;

Find URL of current page and store in application deactivated event

You can find URL of current page in deactivated event as below in Application Deactivated event. Read it and store it in isolated storage.


 private void Application_Deactivated(object sender, DeactivatedEventArgs e)
        {
            Uri currentUri = ((App)Application.Current).RootFrame.CurrentSource;
            isoStorage["data"] = currentUri.OriginalString;

        }

Read and set the start page in application activated event

Next step you need to do is on the application activated event

  1. Read value of current URI from isolated storage
  2. If read value is null then set the MainPage.xaml as start page of the application
  3. Else set the read value as start page of the application

   private void Application_Activated(object sender, ActivatedEventArgs e)
        {

            string CurrentURI = (string)isoStorage["data"];
            if (CurrentURI != "")
            {
                Uri uritoNavigate = new Uri(CurrentURI, UriKind.Relative);
                ((App)Application.Current).RootFrame.Navigate(uritoNavigate);
            }
            else
            {

                 Uri nUri = new Uri("/MainPage.xaml", UriKind.Relative);
                ((App)Application.Current).RootFrame.Navigate(nUri);

            }

You need to perform same steps in application launching event also as of application activated event.


   private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("data") == false)
            {
                isoStorage.Add("data", "/MainPage.xaml");
            }
            string CurrentURI = (string)isoStorage["data"];
            if (CurrentURI != "")
            {
                Uri nUri = new Uri(CurrentURI, UriKind.Relative);

                ((App)Application.Current).RootFrame.Navigate(nUri);
            }
            else
            {

                Uri nUri = new Uri("/MainPage.xaml", UriKind.Relative);
                ((App)Application.Current).RootFrame.Navigate(nUri);

            }

        }

Remove isolated storage value in application closing event


  private void Application_Closing(object sender, ClosingEventArgs e)
        {
            isoStorage["data"] = "";
            isoStorage.Remove("Data");
        }

This is all you need to add in App.Xaml.cs file to perform context switching in your application.

Advertisements

6 thoughts on “Context switching in Windows Phone 7

  1. Unfortunately, you’re giving a lot of really bad advice here. From the Execution Model Best Practices: http://msdn.microsoft.com/en-us/library/ff817009.aspx

    “When the user launches a new instance of your application, it is acceptable to present the user with some information about a previous instance, for example, a list of recently opened documents, but the user should not feel like they are continuing a previous session.”

    Moreover, from the Execution Model Overview: http://msdn.microsoft.com/en-us/library/ff817008.aspx

    “Launching event
    Execute very little code. Do not do resource-intensive operations like accessing isolated storage.”

  2. As a side note, you can probably achieve what you’re looking for here by using PhoneApplicationService.Current.State. Bonus: you don’t need to worry about clearing it when you quit.

  3. Dear Mr. Dhananjay,

    Keep out from giving wrong directions to the developers. As a Microsoft MVP, this kind of articles sound very bad to the community. If you are confident enough writing articles on some specified topic, articulate them properly. Else just stop writing false statements.

    Post the full coverage in a single article and stop splitting them to multiple very small posts just to increase post counts. This irritates the readers. Try to write posts to share knowledge and not to increase post count and blog hit.

    – Pallab R.

  4. @ELI YOUNG Thanks for your suggestion. But I was jut addressing a different scenarion in my tweaking way. However I have put first line of the post as “This post has not adhere to best practice ”

    Thanks keep reading

  5. @Pallab

    I have not given any wrong inforamtion at first place. I agress this post has not given the best practice but boss it is how it is a twaek. this is my blog and I have all right to write my tweaks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s