ActivityGroup 不处理 ListActivity 的后退键
我正在使用 ActivityGroup 生成多个活动,并在 TabActivity 的同一选项卡内切换视图。
当我按下后退键时,会在 ActivityGroup 内调用此方法,
public void back() {
if(history.size() > 0) {
history.remove(history.size()-1);
if (history.size() > 0)
setContentView(history.get(history.size()-1));
else
initView();
}else {
finish();
}
}
此方法允许我保留一堆活动,并在按下后退键时返回到上一个活动。
这在我的所有嵌套活动中都运行良好,但在 ListActivity 上按后退键将简单地退出应用程序。
I am using an ActivityGroup to spawn multiple activities and switch views from within the same tab in a TabActivity.
When I press the back key this method is called inside my ActivityGroup
public void back() {
if(history.size() > 0) {
history.remove(history.size()-1);
if (history.size() > 0)
setContentView(history.get(history.size()-1));
else
initView();
}else {
finish();
}
}
this method allows me to keep a stack of my activities and go back to the previous one when the back key is pressed.
this is working well on all my nested activities, except on a ListActivity where a press on the back key will simply exit the application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 ActivityGroup 中,当 ListActivity 处于焦点时 ActivityGroup 的 onKeyDown() 不会被调用,只有子级(ListActivity)的 onKeyDown() 被调用
为了确保 ActivityGroup 的 onKeyDown() 被调用,我们需要从 ListActivity 的 onKeyDown() 返回 false。完成此更改后,我能够接收关键事件
In a ActivityGroup, When ListActivity is in focus onKeyDown() of ActivityGroup is not getting called, only child's (ListActivity) onKeyDown() is getting called
To make sure ActivityGroup's onKeyDown() is called, We need to return false from onKeyDown() of ListActivity. After doing this change I am able to receive key events
我知道你的意思...几周前我遇到了这个问题。我也知道这是一个烦人的错误,并且我已经吸取了教训:我永远不会使用这种方法!因此,基本上,为了解决这个问题,您必须对代码进行一些解决方法。例如,我通过将以下代码添加到活动之一来解决该问题:
请注意,我的
ActivityGroup
称为StatsGroupActivity
,如下所示:I know what you mean... I faced that problem some weeks ago. I also know it's an annoying bug and I've learned the lesson: I won't use that approach ever! So, basically, in order to fix this you will have to do some workarounds to your code. For instance, I fixed that problem with one of my activities adding this code to the activity:
Notice that my
ActivityGroup
is calledStatsGroupActivity
and looks like:@Cristian
我使用的是普通的Activity而不是ListActivity,但是填充了ListView后它给了我同样的问题。
我仅在 Activity 上实现 onBackPressed 而不是 onKeyDown 来回调 MyActivityGroup 调用的相同 back() 函数。
group 是 MyActivityGroup 中的静态字段。
back() 函数与 yann.debonnel 提供的相同。
我不知道你的 ListActivity 是否是同样的情况,没有测试。但就我而言,它有效。
@Cristian
I am using a normal Activity instead of ListActivity, but with a ListView populated it gave me the same problem.
I only implemented onBackPressed on my Activity instead of onKeyDown to call back the same back() function that MyActivityGroup invoked.
group is a static field in MyActivityGroup.
The back() function would be the same as yann.debonnel provided.
I don't know if this is the same case for your ListActivity, didn't test it. But in my case it worked.