0

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?

Lokesh Mehra
  • 545
  • 6
  • 16
dran
  • 245
  • 1
  • 6
  • 16

5 Answers5

5

put this code....

@Override
    public void onBackPressed() 
    {
        finish();
    }
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
  • 3
    super.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
  • 1
    may 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
  • means you want to change the value of a variable ? – Mehul Ranpara Nov 06 '12 at 13:14
  • 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
0
@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);    

}

Anirudh
  • 2,767
  • 5
  • 69
  • 119
0

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(); 
 }
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
0

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

Community
  • 1
  • 1
Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103
0

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.

cV2
  • 5,229
  • 3
  • 43
  • 53