Android的 - 如何替代"返回]按键使其不能完成()我的活动? [英] Android - How To Override the "Back" button so it doesn't Finish() my Activity?

查看:155
本文介绍了Android的 - 如何替代"返回]按键使其不能完成()我的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个活动,当被展示它通知也将获得显示在通知栏。

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

这是这样,当用户presses家庭和活动得到被推到了后台,他们可以通过通知回到活动。

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.

在出现问题时,用户presses后退按钮,我的活动被破坏,但通知仍然是我希望用户能够preSS回来,但仍然能够通过去的活动通知。但是,当用户尝试这个我得到空指针作为其试图启动一个新的活动,而不是带回旧的。

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.

所以基本上我想后退按钮行为完全相同的Home键,并在这里是如何我已经试过到目前为止:

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;
        }   


不过上述code似乎仍然让我的活动被破坏,我怎么能阻止我的活动从什么时候后退按钮是pressed被破坏?


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

您只需要以下搭上返回键(确保不叫超)

you just need the following to catch back key (make sure not to call super)

另外,如果你计划有在后台服务运行,一定要看看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 android will kill your service if it needs to free memory

@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 true; 
    }
    return super.onKeyDown(keyCode, event);
}


@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的 - 如何替代&QUOT;返回]按键使其不能完成()我的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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