How can I use the Android back button to move back within my app instead of closing my app?

前端 未结 4 702
迷失自我
迷失自我 2021-01-22 18:31

My app has three activities, A, B and C. I am moving from A to B through an OK button, and I want to move back from B to A by using the default back button of Android devices. W

4条回答
  •  别那么骄傲
    2021-01-22 19:04

    why start your activity for result ? when you press the backbutton, the result is set to RESULT_CANCELED form the B activity, so it crashes if you don't handle the resultcode...

    you can handle the backpress like this

    private static final int NONE = -1;
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    
        if (keyCode == KeyEvent.KEYCODE_BACK) {
    
       setResult(NONE, intent);
            finish();
        return super.onKeyDown(keyCode, event);
    }
    

提交回复
热议问题