Android: How to know if an activity is finished?

I’m working on an app that plays mp3 files automatically and in succession from within a given activity. it knows when the current mp3 play is completed by listening for an “onCompletion” event from the MediaPlayer.

However, if I return to the start display by pressing the back button while media is playing, apparently the activity is still running. When the “onCompletion” event is triggered, it tries to access the view within a list activity and crashes on an null pointer exception.

  • How can i work with Android EffectFactory Class?
  • Is Android development restrictive in any way like for the iPhone?
  • onActivityResult doesn't work?
  • Android GPS timeout
  • Android, how to choose save file location?
  • Where is com.google.android.gms?
  • What is the best way to determine that the activity is no longer “active” and therefore be able to prevent the call to play the next audio?

    Related posts:

    Calculate compass bearing / heading to location in Android
    wait until firebase retrieves data
    Display two Toast messages at once?
    Button not responding to Click Event after Translation animation
    Upload photo to Facebook with Facebook Android SDK in Android
    Why Bundle object is always null on onCreate()?
  • Android best practice - views/activities
  • webview cannot load html file from sd card
  • NullPointerException when using LocalBroadcastManager
  • Create Free/Paid versions of Application from same code
  • How to play a Sound Effect in Android
  • Left space in Toolbar with SearchView android studio
  • One Solution collect form web for “Android: How to know if an activity is finished?”

    Try triggering those in the Activity lifecycle methods onStop or onDestroy. Check this documentation for more details.

    onStop will be triggered when user clicks on back button. So handle your events in onStop.

    public void onStop () {
    //do your stuff here
    super.onStop() 
    }
    

    Eventually onDestroy will also be called, but not necessarily as soon as the back button is clicked.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.