locked
Which method of Page is always called when it is displayed? RRS feed

  • Question

  • If you look at the Windows Phone lifecycle, you will notice that OnNavigatedTo() is called when a page is brought to the foreground regardless of the route (app launch or reactivated).

    I assumed it would be the same for OnNavigatedTo() of Windows Store app, but it does not seem to be the case. It is not called when the app is resumed. 

    Is there a method always called when a page is brought to the foreground?


    Hong

    Tuesday, November 26, 2013 3:20 AM

Answers

  • Hi,

    If user switch to other apps from your app, your app suspend. When users bring your app back to the foreground, your app is resumed. So you can implement resumed event in your app just like bilaal says. The event can trigger when user come back to your app. And you can add OnNavigatedTo event in your app page, the event can invoked when the Page is loaded and becomes the current source of a parent Frame.  See Manage app lifecycle and state and How to resume an app

    Best Wishes!



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Anne Jing Friday, November 29, 2013 7:13 AM edit
    • Marked as answer by Hong (MA, USA) Friday, November 29, 2013 2:33 PM
    Friday, November 29, 2013 7:11 AM

All replies

  • Hello Hong, 

    when your page is displayed that one constructor of that page is always call. Like that :

     public MainPage()
            {
                this.InitializeComponent();
            }

    it is always call when your page displayed. If you put your another method after initializecomponent(); than it is called after page initialize all control.

    Tuesday, November 26, 2013 3:52 AM
  • Thank you for your prompt response, Khant.

    Unfortunately, it is not the case for my app.  For example, if I switch to other apps from my app, then come back to it, the constructor is not called. This is also my understanding from the documentation. It is called only when the page is constructed, not when it is resumed. 


    Hong

    Tuesday, November 26, 2013 3:58 AM
  • public App()
    {
            InitializeComponent();
    
            Suspending += OnSuspending;
            this.Resuming += On_resuming;
     }
    private async void On_resuming(object sender, object e)
    {
    
    }
    
    
    thanks bilaal
    Tuesday, November 26, 2013 5:51 AM
  • Thanks, bilaal.

    Does it mean that the method that I am asking for does not exist?  In other words, if I always want to do something when a page is displayed, I must put the action at two places:  OnNavigatedTo and Resuming. 


    Hong

    Tuesday, November 26, 2013 12:07 PM
  • Hi,

    If user switch to other apps from your app, your app suspend. When users bring your app back to the foreground, your app is resumed. So you can implement resumed event in your app just like bilaal says. The event can trigger when user come back to your app. And you can add OnNavigatedTo event in your app page, the event can invoked when the Page is loaded and becomes the current source of a parent Frame.  See Manage app lifecycle and state and How to resume an app

    Best Wishes!



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Anne Jing Friday, November 29, 2013 7:13 AM edit
    • Marked as answer by Hong (MA, USA) Friday, November 29, 2013 2:33 PM
    Friday, November 29, 2013 7:11 AM
  • Thank you all for the tips.  

    Based on the responses, I believe the answer to my question is "No".  No such method exists for Windows Store app. For something to be always done when a page is brought to the front, it has to be put in two places as Anne described.


    Hong

    Friday, November 29, 2013 2:32 PM