Android的 - 如何捕捉的Home键是pressed? [英] Android - How to catch that the Home button was pressed?

查看:133
本文介绍了Android的 - 如何捕捉的Home键是pressed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当用户拥有pressed Home按钮来抓。我以为我可以用

I am trying to catch when the user has pressed the Home button. I thought I could use

protected void onResume()
{
   registerReceiver(homeReceiver, new IntentFilter(Intent.CATEGORY_HOME));
}
...
private BroadcastReceiver homeReceiver = new BroadcastReceiver()
       {
          public void onReceive(Context context, Intent intent)
          {
             // logic here
          }
       };

但似乎并没有工作。据我所知,在onPause将被调用,但我特别的应用程序有一些逻辑,我需要单独处理。短篇小说是基于一些信息进来,应用程序将显示新的活动给用户。为prevent后退按钮的问题,因为每个活动打它的onStop它调用自身完成。但是,如果用户presses Home键,我不想活动调用结束,这样当用户presses主屏幕的最近运行的应用程序列表或者通过应用程序图标,最后一个活动被带回

but that doesn't seem to work. I understand that onPause will be called, but my particular app has some logic that I need to handle separately. Short story is based on some info coming in, the app will display new Activities to the user. To prevent Back button issues, as each Activity hits it's onStop it calls finish on itself. However if the user presses the Home button I do not want the Activity to call finish so that when the user presses the app icon on the home screen or via the Recently run apps list, the last Activity is brought back.

我怀疑是我做了这个复杂得多计划。任何的建议是AP preciated。

I suspect that I have made this more complicated than planned. Any suggestions are appreciated.

推荐答案

长话短说是我放弃了追赶的主页按钮是pressed。我假设它是完成(),其每一项活动是在自己的onStop这是问题,呼吁本身。因为我还需要调用finish()上的每个活动,以避免后退按钮问题我更新了B至第一个揭开序幕,新的活动与startActivityForResult,然后立即拨打finishActivity在previous活动。这似乎已经解决了整个问题。现在,当我preSS Home键,并尽量减少应用程序,当我从主屏幕上选择应用程序的图标或家庭的最近运行的应用程序列表中,我得到了正确的活动带到了前台。

Long story short is that I gave up on trying to catch that the Home button is being pressed. I'm assuming that it is the finish() that each Activity was calling on itself in their onStop that is the issue. Since I still need to call finish() on each Activity to avoid back button issues I updated B to first kick off the new Activity with startActivityForResult and then immediately call finishActivity on the previous Activity. This appears to have solved the overall issue. Now when I press the Home button and minimize the app, when I select the app's icon from the Home screen or Home's recently run apps list I get the correct Activity brought to the foreground.

现在我只需要找出原因 - 有时 - pressing Home键是造成我的服务停止。但是,这将是一个新的问题,如果我不能得到它想通了。谢谢大家对你的想法。

Now I just need to figure out why - sometimes - pressing the Home button is causing my Services to stop. But that will be a new question if I can't get it figured out. Thank you everyone for your ideas.

这篇关于Android的 - 如何捕捉的Home键是pressed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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