Android-避免破坏活动,只需在按下“后退"按钮时停止或暂停该活动 [英] Android - Avoiding an activity to destroy, just stopping or pausing it when pushing the back button

查看:100
本文介绍了Android-避免破坏活动,只需在按下“后退"按钮时停止或暂停该活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下返回按钮时,我想暂停或将应用程序置于后台,我不希望应用程序进入销毁状态.问题是当我重写onKeyDown并强制使用onPause暂停或停止应用程序时,唤醒锁和应用程序崩溃时会出现一些问题,但是当我按下主屏幕按钮时,我会使用onPause方法,而且我也没有例外,这很奇怪!!

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!!

推荐答案

答案是

public boolean onKeyDown(int keyCode, KeyEvent event)
{

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

    return false;
}

这篇关于Android-避免破坏活动,只需在按下“后退"按钮时停止或暂停该活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆