开发者

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

开发者 https://www.devze.com 2022-12-24 00:44 出处:网络
I would like to pa开发者_C百科using 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 on

I would like to pa开发者_C百科using 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!!


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

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


The answer is

public boolean onKeyDown(int keyCode, KeyEvent event)
{

    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        moveTaskToBack(true);
        return true; // return
    }

    return false;
}


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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消