开发者

How can i listen the BACK keydown in MaintabActivity (contains 2 sub tabActivities)

开发者 https://www.devze.com 2023-03-18 02:00 出处:网络
public class MyTab extends TabActivity; public class SubTab extends TabActivity; There is 2 tab defined in MyTab :

public class MyTab extends TabActivity;

public class SubTab extends TabActivity;

There is 2 tab defined in MyTab :

  1. setContent(new Intent(this, SubTab.class))
  2. setContent(R.id.view1)

Then write a method in MyTab :

public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        Log.i("MyTab Back", "In MyTab ");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

and the method in SubTab ,just change to Log.i("SubTab Back", "In SubTab ").

Problem: Press Back Key in SubTab,Only "In SubTab" is shown in Logcat.

Delete the method in SubTab,then press Back Key开发者_如何学运维 nothing is shown in Logcat.

Press Back Key in another tab (not Activity), Only "In MyTab" is shown in Logcat.

NOW, I want to just write a method to listen Back Keydown in MyTab, because of in another project, the mainTab have more than 3 subtabActivity 。


try the following:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        Log.i("MyTab Back", "In MyTab ");
    }
    return super.dispatchKeyEvent(event);
}


try this :

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Log.i("MyTab Back", "In MyTab ");
        return true;
    }
    return false;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号