Android - Avoiding an activity to destroy, just stopping or pausing it when pushing the back button

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

问题:

I would like to pausing or putting the application on background when pressing the back button, I don't want the application to go through the destroy state. Things are when I override onKeyDown and when I force to pause or stop the application by using onPause, I have some issuees with the wakelock and application crash, but when I press home button I go through onPause method and I have no exception, it's weird!!

回答1:

Try to use the method onBackPressed(), like this:

@Override public void onBackPressed() {     moveTaskToBack(true); } 


回答2:

The answer is

public boolean onKeyDown(int keyCode, KeyEvent event) {      if (keyCode == KeyEvent.KEYCODE_BACK)     {         moveTaskToBack(true);         return true; // return     }      return false; } 


回答3:

@Override public void onBackPressed() {     if(mWebView.canGoBack())         mWebView.goBack();     else         //super.onBackPressed();         moveTaskToBack(true);  } 


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