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?
问题:
回答1:
put this code....
@Override public void onBackPressed() { finish(); }
回答2:
@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);
}
回答3:
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(); }
回答4:
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
回答5:
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.