片段pressing后退按钮片段、按钮、pressing

2023-09-13 23:40:17 作者:丢了!路

我现在有包含片段的活性

[1],[2],[3],[4]

如果pressing按钮,[3],可以被重定向至[4]

我想实现后退按钮,如图跟随。

在pressing回来[4],它返回[3]

我的电脑后退按键不能用了

在pressing回[3],它返回[2]

在pressing回来[1],活动结束();

当谈到目前的实现,它完成雨后春笋般冒出来的片段的活性来代替。请你告诉我,我应该做的还是记住?

  @覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){

    如果(键code == KeyEvent.KEY code_BACK)
    {

        完();
    }

        返回super.onKeyDown(键code,事件);

}
 

解决方案

这对我来说是一个有效的解决方案:

  dialog.setOnKeyListener(新DialogInterface.OnKeyListener(){
                    @覆盖
                    公共布尔onKey(DialogInterface对话,诠释键code,KeyEvent的事件){
                        如果(键code == KeyEvent.KEY code_BACK){
                            //做你想做的背面preSSED
                            返回true;
                        }
                        返回false;
                    }
                });
 

I am now having an activity containing fragments

[1] , [2] , [3] , [4]

If pressing buttons , [3] , it can be redirected to [4]

I would like to implement the back button as shown follow..

when pressing back at [4] , it return to [3]

when pressing back at [3] , it return to [2]

when pressing back at [1] , the activity finishes();

When it comes to the current implementation, it finish the activity instead of popping up the Fragment. Would you please tell me what I should do or keep in mind ?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if( keyCode==KeyEvent.KEYCODE_BACK) 
    {   

        finish();
    }       

        return super.onKeyDown(keyCode, event); 

}   

解决方案

This is a working solution for me:

dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_BACK) {
                            // DO WHAT YOU WANT ON BACK PRESSED
                            return true;
                        }
                        return false;
                    }
                });