locked
Determining whether app is deactivating or not RRS feed

  • Question

  • I have an app with a page in which the user fills out several fields. Some of the properties of this page I need to save in state when the app is deactivated, but not when it is closed (I need to remember them if the phone is turned off or Start button pressed, but not if the user hits the Back button or the cancel or submit buttons on the page). I know that the App class has Application_Deactivated and Application_Closing, but if I were to use these how would I access the data from the current page? What I would really like to be able to do is have a way to determine, from within OnNavigatedFrom, whether the app is being deactivated or if the user is navigating away from the page. Can somebody please help me here? Thanks.
    Friday, December 9, 2011 8:38 PM

Answers

  • In Application_Deactivated, you can obtain the current page and test for a particular one like this:

                PhoneApplicationPage page = (App.Current.RootVisual as PhoneApplicationFrame).Content as PhoneApplicationPage; 
                if (page is MainPage) 
                { 
     
                } 
                else if (page is DetailsPage) 
                { 
     
                } 


    Richard Woo
    Friday, December 9, 2011 9:51 PM

All replies

  • In Application_Deactivated, you can obtain the current page and test for a particular one like this:

                PhoneApplicationPage page = (App.Current.RootVisual as PhoneApplicationFrame).Content as PhoneApplicationPage; 
                if (page is MainPage) 
                { 
     
                } 
                else if (page is DetailsPage) 
                { 
     
                } 


    Richard Woo
    Friday, December 9, 2011 9:51 PM
  • how would I access the data from the current page?


    First of all extending the solution suggested by Richard if you still wondering about accessing data.

            private void Application_Deactivated(object sender, DeactivatedEventArgs e) 
            { 
                PhoneApplicationPage page = (App.Current.RootVisual as PhoneApplicationFrame).Content as PhoneApplicationPage; 
                if (page is MainPage) 
                { 
                    MainPage mainPage = (MainPage)page; 
     
                    //testData is a public variable defined in your MainPage 
                    int data = mainPage.testData; //This is to demonstrate how to acess data 
                } 
            } 

    Other possible solution (holds good, if small amount of data) would be defining a global variable in App.xaml file and update that variable on MainPage.cs then at the time of deactivation use that variable to save data. But I would still suggest to use first method. Rest depends what you can implement more conveniently in your app :-)

    Thanks and Regards
    Nishant Rana
    Saturday, December 10, 2011 5:45 PM