androideventsback-buttonpressed

Handle back button pressed in BaseGameActivity or SurfaceView


When I created my MainActivity class, I change the "extends" to extends BaseGameActivity to implement Google+ login. There are some downsides to this. I can no longer handle onBackPressed() and onTouchEvent(MotionEvent e). This kind of sucks.

Fixing onTouchEvent was easy. My game uses SurfaceView which can take touch input. But for onBackPressed, I cannot simply do this.

Anyone know how to handle back button pressed events. I saw there was a onKeyDown() method in both surfaceview and basegameactivity but from prior experience, I'm pretty sure that is only for devices with API version 5 and older, right? Someone know how to do this?


Solution

  • is not called because it is only in Activity
    

    You are actually using the support version of the Activity which is the FragmentActivity which you can call onBackpressed programatically.

    sample:

    You can create a method for onBackPress functionality and call it whenever you want. But Also I really think that it should be called upon pressing the back button because it is a support version of activity means it extends from Activity under the hood of it.

    BaseGameActivity.this.onBackPressed();