Key Events in TabActivities?

允我心安 提交于 2019-11-28 08:49:23
Steve Zeng

It turns out to be pretty easy. Add the following code to your child tab activity :

 @Override
  public void onBackPressed() {
    this.getParent().onBackPressed();   
  }

Then in the TabActivity do the real logic:

 @Override
  public void onBackPressed() {
    // Called by children
  }

Otherwise, the children will intercept and consume the event without notifying the tab host.

I had the same issue and found overriding dispatchKeyEvent worked.

An example of which can be found here for back button press:

http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html

Each tab's Activity handled the "back" presses.

I have a TabActivity and want to catch and handle presses of HOME and BACK. Where do I need to catch these events?

You cannot "handle presses of HOME", ever.

With respect to BACK, you can use onKeyDown() (for Android 1.x) or onBackPressed() (for Android 2.x). However, your TabActivity may be too late. For example, if you have activities as the contents of your tabs, it may be that one of them is catching the BACK press and arranging for normal processing (i.e., closing up of the activity). Since I avoid activities-as-tabs like the plague (except for one book example), I have not experimented with BACK button processing in that scenario.

patric

try this in your oncreate()

setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_GLOBAL);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!