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.

  • Turn ON/OFF Camera LED/flash light in Samsung Galaxy Ace 2.2.1 & Galaxy Tab
  • How to use “Send Feeback”, FeedbackActivity, in Android?
  • How to programmatically add view in ViewFlipper
  • Getting the size of the window WITHOUT title/notification bars
  • Check fragment is currently visible or not in android?
  • RelativeLayout is taking fullscreen for wrap_content
  • 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:

    How to detect when an Android app goes to the background and come back to the foreground
    LocationManager's getBestProvider returning null
    Custom animations for starting activities in Android not working as expected
    How to change the marker for the overlay on tap for android?
    Android SDK tools install in Docker fails
    Windowmanager with Animation
  • How can I get an Android MediaController to appear from layout xml?
  • Vibrate setting not turning off on receiving incoming call - Android
  • How to clear distinct_id in mixpanel android when user logs out?
  • ImageLoader vs Android Universal Image Loader
  • EditText: Avoid getting focus on setText()
  • Use two pendingIntent on one view in Appwidget
  • 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

    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.