Skip to content Skip to sidebar Skip to footer

Android: Mediaplayer Plays Multiple Times If I Keep On Pressing The Back Button And Launch The App Again

When I play my song and press the back button to return to return to home, the music continues playing. When I launch the app again, the music plays twice. I think it's the onResum

Solution 1:

I'm not really sure about the behavior you want. As I see it, it's one of two:

  1. Music should be played only while my Activity is visible

    If this is the case, you should look closer on the documentation for the Activity Lifecycle. This will for example tell you that onResume() will also be called the first time the Activity is created.

    So the solution would be to start the music in onResume() and stop on onPause() or similar.

  2. After starting my activity once, music should be played even if i return to home screen

    In this case you really want a Service.

Post a Comment for "Android: Mediaplayer Plays Multiple Times If I Keep On Pressing The Back Button And Launch The App Again"