Search code examples
windows-phone-7pivottombstoning

Windows Phone 7 : resume to the selected pivot item


As i'm using the Pivot control in my app, I wonder how can I resume to the last selected pivot item after the user tombstoned the app (Launched the App, pressed the windows button and pressend the back button to resume)?

(I tried to add some code in the Application_Deactivated and Application_Deactivated but didn't work)


Solution

  • To save the state of the Pivot you should be using the State property of the page in the OnNavigatedTo and OnNavigatedFrom methods.

    Here is a basic example:-

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (State.ContainsKey("pivotIndex"))
                myPivot.SelectedIndex = (int)State["pivotIndex"];
        }
    
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            State["pivotIndex"] = myPivot.SelectedIndex;
        }
    

    Note that the Windows Phone will handle the persisting of this state in the case where your application gets tombstoned. This approach also enables your page to navigate to elsewhere in the app and on navigation back your pivot state is restored.