How can i listen the BACK keydown in MaintabActivity (contains 2 sub tabActivities)
public class MyTab extends TabActivity;
public class SubTab extends TabActivity;
There is 2 tab defined in MyTab :
- setContent(new Intent(this, SubTab.class))
- 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;
}
精彩评论