Search code examples
androidback-buttonvirtual-realityoculusgear-vr

Gear VR Framework Override Back Button Functionality


We are developing a Gear VR Application using GVRf. There are two GVRScenes in the app, and we want it such that if the user short presses back button in scene B it goes back to scene A, and if the user short presses back button in scene A it exits the app. Also, long pressing the back button should show the Oculus settings menu, and pressing back from that should resume the app(according to Oculus store requirements).

Since GVRActivity.onBackPressed() is not called from the framework, we tried to override the onKeyUp from GVRActivity:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (KeyEvent.KEYCODE_BACK == keyCode) {
        if (mScript.onBackPressed()) {//mScript.onBackPressed() handles change scene logic 
                                      //and returns true if we are in scene B, and returns  
                                      //false if we are in scene A
             super.onBackPressed();
        }
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

Right now short press from scene B is ok, and long press is showing the oculus settings. The problem is that if we press back from scene B and press back from settings, it switches to scene A, and if we do the same from scene A the app restarts.

What is the correct way to implement this? Thanks in advance.


Solution

  • We found a work-around for this.

    First, modify GVRActivity's mPaused to protected.

        protected boolean mPaused = true;
    

    And then we added overrode the onKeyUp() in our Activity:

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyUp: "+keyCode);
        if(!mPaused && keyCode==KeyEvent.KEYCODE_BACK){
            if(!mScript.onBackPressed())
                return true;
        }
        return super.onKeyUp(keyCode, event);
    }
    

    Hope this can help someone else, if anyone has a better solution, feel free to post it.