2

I want below keyevents into all activity. I have approx 15 activities.

    @Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_D:
            moveShip(MOVE_LEFT);
            return true;
        case KeyEvent.KEYCODE_F:
            moveShip(MOVE_RIGHT);
            return true;
        case KeyEvent.KEYCODE_J:
            fireMachineGun();
            return true;
        case KeyEvent.KEYCODE_K:
            fireMissile();
            return true;
        default:
            return super.onKeyUp(keyCode, event);
    }
}

Instead of writing same code into all activity. I want to write it once & use it in all activities.

How can I achieve this scenario?

NovusMobile
  • 1,813
  • 2
  • 21
  • 48
  • 3
    Create a Base activity that will be extended by all other activities. Put logic common to all activities in base activity. – SachinGutte Apr 22 '15 at 12:23

1 Answers1

0

Create one BaseActivity which extends Activity where you can put this method. Extend that BaseActivity all your 15 activities. In all your activities you just need to add :

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
  super.onKeyUp(int keyCode, KeyEvent event);
 }

This will call your BaseActivity onKeyUp method.

Karn Shah
  • 501
  • 4
  • 14