Hit the back button but not kill the activity and let it be in Back Stack (Android)

前端 未结 2 1475
难免孤独
难免孤独 2021-01-25 03:48

I have 3 activities A, B and C.

I inflate activity B dynamically on a button click and the user can add as many views as he likes.

The operation is like this:

2条回答
  •  深忆病人
    2021-01-25 04:34

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    
            switch(keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                //bring back the previous activity do your logic here           
                return false;
            }
        return super.onKeyDown(keyCode, event);
    }
    

    You could use this, return false when activity should not be closed. use FLAG_ACTIVITY_REORDER_TO_FRONT to launch the previous activity

提交回复
热议问题