不要在 onBackPressed 上调用 onDestroy

发布于 2024-11-10 13:51:09 字数 1471 浏览 5 评论 0原文

我不希望当按下“后退”按钮时我的活动被破坏。我的应用程序与 1.6 SDK 兼容。参考 http:// android-developers.blogspot.com/2009/12/back-and-other-hard-keys- Three-stories.html覆盖后退按钮以充当主页按钮,我选择了以下代码:

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
                // For versions lower than 2.0     
    if (Utility.buildDet.getDeviceBuildAPI() <= Utility.buildDet.getBuildApi() 
            && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
        onBackPressed();
    return super.onKeyDown(keyCode, event);
}

      // In any version, this function will be called
public void onBackPressed() {
    // This will be called either automatically for you on 2.0    
    // or later, or by the code above on earlier versions of the platform.
    Log.i(TAG, "##### BACK KEY IS PRESSED");
    this.moveTaskToBack(true);  // on false, it shows moveTaskToBack: 11
    return;
}

这些日志

: ##### BACK KEY IS PRESSED
 INFO/ActivityManager(51): moveTaskToBack: 10
: !!!!!!! Into onPause
: !!!!!!! Into onStop
: !!!!!!! Into DESTROY

当我按后退按钮时,我没有覆盖 moveTasToBack()。 Anu clu 我该怎么做才能在按下后退按钮时不被破坏。也许我想忽略按钮或隐藏活动。

任何线索,为什么它没有按预期工作。

谢谢

I don't want my acivity to get destroyed when Back button is pressed. My app is compatible from 1.6 SDK's. Referring to http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html and Override back button to act like home button, I opted the following code :

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
                // For versions lower than 2.0     
    if (Utility.buildDet.getDeviceBuildAPI() <= Utility.buildDet.getBuildApi() 
            && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
        onBackPressed();
    return super.onKeyDown(keyCode, event);
}

      // In any version, this function will be called
public void onBackPressed() {
    // This will be called either automatically for you on 2.0    
    // or later, or by the code above on earlier versions of the platform.
    Log.i(TAG, "##### BACK KEY IS PRESSED");
    this.moveTaskToBack(true);  // on false, it shows moveTaskToBack: 11
    return;
}

When I press Back button, I these logs

: ##### BACK KEY IS PRESSED
 INFO/ActivityManager(51): moveTaskToBack: 10
: !!!!!!! Into onPause
: !!!!!!! Into onStop
: !!!!!!! Into DESTROY

I have not overridden moveTasToBack(). Anu clu what do I do to not get destroyed when back button is pressed. Maybe I want to just ignore the button or hide the activity.

Any clue, why it is not working as expected.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨落成白 2024-11-17 13:51:09
public boolean onKeyDown(int keyCode, KeyEvent event)  
{

     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
     {

        this.moveTaskToBack(true);
           return true;
      }

    return super.onKeyDown(keyCode, event);
}
public boolean onKeyDown(int keyCode, KeyEvent event)  
{

     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
     {

        this.moveTaskToBack(true);
           return true;
      }

    return super.onKeyDown(keyCode, event);
}
夏夜暖风 2024-11-17 13:51:09

为什么要改变标准系统行为? Android 系统可以破坏 Activity,也可以不破坏 Activity。它应该仅取决于系统。您可能想做一些可以以不同方式完成的事情。也许您需要服务或实施保存活动状态?

Why do you want to change standard system bahaviour? Android system can destroy activity or not. It should depend only on the system. You probably want to do something that can be done differently. Maybe you need services or implement saving state of activity?

属性 2024-11-17 13:51:09
@Override
public void finish() {
    //super.finish();
    moveTaskToBack(true);
}
@Override
public void finish() {
    //super.finish();
    moveTaskToBack(true);
}
梦里寻她 2024-11-17 13:51:09

捕获返回键并不能解决这个问题。 Android 中的 Activity 生命周期最好留给系统。系统会随时终止您的活动。

Capturing back key won't solve this. Activity Life cycle in android is best left to the System. System will kill your activity whenever it wants.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文