locked
What happens when Windows Button is pressed? RRS feed

  • Question

  • This probably is a very simple question but I can't seem to find the answer anywhere so here you go...

    What happens when the Windows Button (Home button?) is pressed in the app? Is there an event that fires off that I can try to use to handle an operation I need to handle before it closes?

    My main problem is this. I couldn't find any way to play a user's playlist except for MediaPlayer (please correct me if there is another way!) so when the home button is pressed, the music continues... I would like to stop the music before exiting to the home screen.

    Thank you for all of your help!


    -JR

    Saturday, November 2, 2013 1:58 AM

Answers

  • You could handle the OnNavigated Events, they basically tell your app, "Hey, so I am going to leave you now and attend another place, is there anything you want me to do before I put you in the background?" and that's where you would code your doing such as "MediaPlayer.Pause" 

    C#

     protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            MediaPlayer.Resume; //To Resume Playing the song when you return to your app
            base.OnNavigatedTo(e);
        }

    Now don't get me wrong, this isn't for the app, this is for the page, basically you handle each of these in their respective pages, this will make it more flexible as coding machine. 
    you could also find more really important code in the App.xaml.cs (or App.xaml.vb) and in there you will find code for the entire application. 

    I can give an example in VB (I know, dead language I am told often, so old, but isn't english old? :P)

      ' Code to execute when the application is launching (eg, from Start)
        ' This code will not execute when the application is reactivated
        Private Sub Application_Launching(ByVal sender As Object, ByVal e As LaunchingEventArgs)
        End Sub
    
        ' Code to execute when the application is activated (brought to foreground)
        ' This code will not execute when the application is first launched
        Private Sub Application_Activated(ByVal sender As Object, ByVal e As ActivatedEventArgs)
        End Sub
    
        ' Code to execute when the application is deactivated (sent to background)
        ' This code will not execute when the application is closing
        Private Sub Application_Deactivated(ByVal sender As Object, ByVal e As DeactivatedEventArgs)
        End Sub
    
        ' Code to execute when the application is closing (eg, user hit Back)
        ' This code will not execute when the application is deactivated
        Private Sub Application_Closing(ByVal sender As Object, ByVal e As ClosingEventArgs)
        End Sub

    You can find more on navigation from anywhere, but here is a place to start: http://www.silverlightshow.net/items/Windows-Phone-7-Part-3-Understanding-navigation.aspx


    • Edited by superhero-man Saturday, November 2, 2013 2:22 AM
    • Proposed as answer by sm_on_live Monday, November 4, 2013 2:54 PM
    • Marked as answer by JROG8706 Monday, November 4, 2013 8:20 PM
    Saturday, November 2, 2013 2:21 AM

All replies

  • You could handle the OnNavigated Events, they basically tell your app, "Hey, so I am going to leave you now and attend another place, is there anything you want me to do before I put you in the background?" and that's where you would code your doing such as "MediaPlayer.Pause" 

    C#

     protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            MediaPlayer.Resume; //To Resume Playing the song when you return to your app
            base.OnNavigatedTo(e);
        }

    Now don't get me wrong, this isn't for the app, this is for the page, basically you handle each of these in their respective pages, this will make it more flexible as coding machine. 
    you could also find more really important code in the App.xaml.cs (or App.xaml.vb) and in there you will find code for the entire application. 

    I can give an example in VB (I know, dead language I am told often, so old, but isn't english old? :P)

      ' Code to execute when the application is launching (eg, from Start)
        ' This code will not execute when the application is reactivated
        Private Sub Application_Launching(ByVal sender As Object, ByVal e As LaunchingEventArgs)
        End Sub
    
        ' Code to execute when the application is activated (brought to foreground)
        ' This code will not execute when the application is first launched
        Private Sub Application_Activated(ByVal sender As Object, ByVal e As ActivatedEventArgs)
        End Sub
    
        ' Code to execute when the application is deactivated (sent to background)
        ' This code will not execute when the application is closing
        Private Sub Application_Deactivated(ByVal sender As Object, ByVal e As DeactivatedEventArgs)
        End Sub
    
        ' Code to execute when the application is closing (eg, user hit Back)
        ' This code will not execute when the application is deactivated
        Private Sub Application_Closing(ByVal sender As Object, ByVal e As ClosingEventArgs)
        End Sub

    You can find more on navigation from anywhere, but here is a place to start: http://www.silverlightshow.net/items/Windows-Phone-7-Part-3-Understanding-navigation.aspx


    • Edited by superhero-man Saturday, November 2, 2013 2:22 AM
    • Proposed as answer by sm_on_live Monday, November 4, 2013 2:54 PM
    • Marked as answer by JROG8706 Monday, November 4, 2013 8:20 PM
    Saturday, November 2, 2013 2:21 AM
  • THanks for the help! I believe i need the OnNavigatedFrom method to be implemented. I'll test it when I get home but from the link you provided I believe thats it! Thanks for your help!

    -JR

    Monday, November 4, 2013 8:20 PM
  • You really should handle this in the Application_Deactivated and Application_Closing handlers rather than in the page-level events.

    This is a good article on the Windows Phone application lifecycle:

    http://developer.nokia.com/Community/Wiki/Application_Life_Cycle_-_Windows_Phone


    -- George Roberts (sirtwist)

    Monday, November 4, 2013 8:37 PM
  • No prob. I am glad i could help, I learned the hard way ahaha, but yeah. Good luck
    Wednesday, November 6, 2013 5:45 AM