5
votes

For a Google Glass App, how do you make you app gracefully exit on a downswipe to the main glass menu?

Trying this code in MainActivity:


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent().getBooleanExtra("EXIT", false)) 
        {
            finish();
            return;
        }       
    }

    @Override
    public boolean onKeyDown(int keycode, KeyEvent event) {
        if (keycode == KeyEvent.KEYCODE_BACK){
            Intent intent = new Intent(MainActivity.this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("EXIT", true);
            startActivity(intent);        }
        return false;
    }


This doesn't seem to "exit" a Google Glass app. This may work for Android apps but this causes a Glass app to enter a weird state where a blank screen is shown permanently. I want to "exit" and see the main Google glass interface or exit the app and put the glass into it's default sleep mode.

1

1 Answers

-1
votes

All you need to do is call finish() on the keydown.

public boolean onKeyDown(int keycode, KeyEvent event) {
    if (keycode == KeyEvent.KEYCODE_BACK){
        finish();        }
    return false;
}

public void onDestroy() {
    super.onDestroy();

}

You might face a problem with the onKeyDown method, use gesture detector in that case.

if (gesture == Gesture.SWIPE_DOWN) {
                finish();
                return true;
            }

You can check here for complete reference.