onBackPressed to kill a handler within an activity - Android

心已入冬 提交于 2019-12-05 01:16:16
Arun George

Now there are couple of wrong things in your code:

  1. If you want to go back to your previous activity, you do not relaunch it using an startActivity as the default behaviour of android is that it maintains a stack of previous activities. On press of back it will by default go back to the previous activity (unless you have launched the child activity using some flags.)

  2. Why are you using a System.exit(0); on back press? You just have to call the finish() to finish the current activity.

  3. The best way to remove callbacks from a handler is using a null as a parameter. You may try the following code:

    mHandlerNextfile.removeCallbacksAndMessages(null);
    mHandlerWholeLesson.removeCallbacksAndMessages(null);
    

    since this would remove all callbacks. Check this link to know more: http://developer.android.com/reference/android/os/Handler.html#removeCallbacksAndMessages(java.lang.Object)

Your Handler isn't "running", your media player is. Just call mp.stop() in onBackPressed().

Also, what Arun George said.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!