onBackPressed not close Current Activity In Android

匿名 (未验证) 提交于 2019-12-03 00:44:02

问题:

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.



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