I created an app for playing videos. It works fine but the problem is - when I press the back button on the device the Activity is not closing. The video is playing in the Background. I am using onBackpressed()
. How to close the current activity?

- 545
- 6
- 16

- 245
- 1
- 6
- 16
-
can you post your onBackPressed method? – Nermeen Nov 06 '12 at 11:59
-
public void onBackPressed() { super.onBackPressed(); finish(); } – dran Nov 06 '12 at 12:00
-
did you call `super.onBackpressed()`??? – David M Nov 06 '12 at 12:00
-
1also stop your player `if(mPlayer.isPlaying()) mPlayer.stop();` on `onBackPressed()` – Mohammed Azharuddin Shaikh Nov 06 '12 at 12:01
-
Are you loading a flash video in webview ? – Vrashabh Irde Nov 06 '12 at 12:12
-
i play the i frame embedded videos – dran Nov 06 '12 at 12:13
5 Answers
put this code....
@Override
public void onBackPressed()
{
finish();
}

- 4,245
- 2
- 26
- 39
-
3super.onBackPressed() calls finish() anyway, so you only need one or the other. – Joss Stuart Nov 06 '12 at 12:03
-
I tried this But not working .the sound is audiable .I heard the video Sound – dran Nov 06 '12 at 12:13
-
1may be this link will help you...http://stackoverflow.com/questions/5946698/how-to-stop-youtube-video-playing-in-android-webview – Mehul Ranpara Nov 06 '12 at 12:16
-
given code is working perfectly in my application..so, use this for close ordinary activity on back button pressed. – Mehul Ranpara Nov 06 '12 at 12:52
-
-
if you change the value in second activity then it is obvious changed....dear..because it is declared as static...but if you not changed your value in second activity then it is remains the same as first activity value.. – Mehul Ranpara Nov 06 '12 at 13:34
-
yes..take a new variable and assign the static variable value to it when going to next activity – Mehul Ranpara Nov 06 '12 at 14:18
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19172/discussion-between-user1597094-and-mehul1000) – dran Nov 06 '12 at 18:58
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//close you app or do what ever you want
}
return super.onKeyDown(keyCode, event);
}

- 2,767
- 5
- 69
- 119
You have two way for this First :
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// TODO Auto-generated method stub
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
super.finish();
}
return super.onKeyDown(keyCode, event);
}
Second
@Override public void onBackPressed()
{
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}

- 15,962
- 9
- 47
- 85
-
-
then you should put some more explanation, for both the line so the newer user not get confuse, and off-course not for POINTS; Technicality always welcome. – Anand Tiwari Nov 06 '12 at 12:46
-
Well try this
@Override
protected void onDestroy() {
super.onDestroy();
final WebView webview = (WebView)findViewById(R.id.YourWebView);
webview.stopLoading();
webview.loadData("", "text/html", "utf-8");
}
Similar here: android WebView stop Flash plugin onPause and Embedded IFRAME video keeps playing in background after exiting fullscreen in Android
This one would also help if the above doesnt work: How to stop youtube video playing in Android webview?
This link basically explains a thread bug in Android webview which was asked here: WebView threads never stop (WebViewCoreThread, CookieSyncManager, http[0-3]) - using reflection to access onPause and onResume methods to kill your running threads

- 1
- 1

- 14,129
- 6
- 51
- 103
You have a different problem, you should not add a workaround to solve this. Something is catching your onBackPressed - Or your onBackPressed is catched by something, that's why its not working.

- 5,229
- 3
- 43
- 53