Activity的onKeyDown方法

HomeActivity:

public class HomeActivity extends BaseActivity implements View.OnClickListener, OnExtraPageChangeListener

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   // 返回false是不消费该事件,后面的监听,这里是BaseActivity(extends BaseActivity)也能得到该事件
   // 返回true是消费,后面的监听得不到该事件了
   if (keyCode == KeyEvent.KEYCODE_BACK) {
      return false;
   }
   return true;
}

BaseActivity:

public abstract class BaseActivity extends FragmentActivity
 
  
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // 在父Activity中退出了程序
        finish();
        return true;
    }
    return false;
}

你可能感兴趣的:(Android开发)