Android - 如何覆盖“返回"按钮,所以它不会完成()我的活动? [英] Android - How To Override the "Back" button so it doesn't Finish() my Activity?

查看:26
本文介绍了Android - 如何覆盖“返回"按钮,所以它不会完成()我的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个活动,当它显示时,通知栏也会显示通知.

I currently have an Activity that when it gets displayed a Notification will also get displayed in the Notification bar.

这样当用户按下 home 并且 Activity 被推到后台时,他们可以通过通知返回到 Activity.

This is so that when the User presses home and the Activity gets pushed to the background they can get back to the Activity via the Notification.

当用户按下后退按钮时会出现问题,我的活动被破坏但通知仍然存在,因为我希望用户能够按下后退但仍然能够通过通知进入活动.但是当用户尝试这个时,我得到空指针,因为它试图开始一个新的活动,而不是带回旧的活动.

The problem arises when a User presses the back button, my Activity gets destroyed but the Notification remains as I want the user to be able to press back but still be able to get to the Activity via the Notification. But when a USER tries this I get Null Pointers as its trying to start a new activity rather than bringing back the old one.

所以基本上我希望后退按钮的作用与主页按钮完全相同,这是我迄今为止尝试过的方法:

So essentially I want the Back button to act the exact same as the Home button and here is how I have tried so far:

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event)  {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
                    && keyCode == KeyEvent.KEYCODE_BACK
                    && event.getRepeatCount() == 0) {
                Log.d("CDA", "onKeyDown Called");
                onBackPressed();
            }

            return super.onKeyDown(keyCode, event);
        }

        public void onBackPressed() {
            Log.d("CDA", "onBackPressed Called");
            Intent setIntent = new Intent(Intent.ACTION_MAIN);
            setIntent.addCategory(Intent.CATEGORY_HOME);
            setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(setIntent); 

            return;
        }   

<小时>

但是上面的代码似乎仍然允许我的Activity被销毁,如何在按下后退按钮时阻止我的Activity被销毁?


However the above code still seems to allow my Activity to be destroyed, How can I stop my Activity from being destroyed when the back button is pressed?

推荐答案

当你有 KEY_BACK 时,移除你的关键监听器或返回 true.

Remove your key listener or return true when you have KEY_BACK.

您只需要以下内容即可捕获返回键(确保不要在 onBackPressed() 中调用 super).

You just need the following to catch the back key (Make sure not to call super in onBackPressed()).

此外,如果您计划在后台运行服务,请务必查看 startForeground() 并确保有持续的通知,否则 Android 会在需要时终止您的服务释放内存.

Also, if you plan on having a service run in the background, make sure to look at startForeground() and make sure to have an ongoing notification or else Android will kill your service if it needs to free memory.

@Override
public void onBackPressed() {
   Log.d("CDA", "onBackPressed Called");
   Intent setIntent = new Intent(Intent.ACTION_MAIN);
   setIntent.addCategory(Intent.CATEGORY_HOME);
   setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(setIntent);
}

这篇关于Android - 如何覆盖“返回"按钮,所以它不会完成()我的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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