Android: intercepting back key

偶尔善良 提交于 2019-12-23 15:36:10

问题


since the back key destroys my application and all data will be lost I need to intercept it to ask the user if that is really what he wants.

I thought of the following structure:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {    
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
        { 
         // ask user if he really wants to exit
         // no --> return true;
         // yes --> return super.onKeyDown(keyCode, event);
         //manually entering either of the return values works fine
        }   
    return super.onKeyDown(keyCode, event);
    }

The "ask user" piece I wanted to realize using an alert dialog. My problem is now that the alert dialog is displayed but that the onKeyDown method runs to the end while the alert dialog is shown and that within the alert dialog I have no idea how to tell the system to pass the right return value.

The complete code I had in mind is

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {    
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
        { 

            alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Tile");
            alertDialog.setMessage("data lost, are you sure?");

            alertDialog.setButton(-1, getString(R.string.yes), new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which) 
                {
                    return;
                    //I only can return without a boolean value here.                   }
            });

            alertDialog.setButton(-2, getString(R.string.no), new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which) 
                {
                    return;
                }
            });

            alertDialog.show();
        }   
        return super.onKeyDown(keyCode, event);
    }

Thanks, A.


回答1:


When the user presses back your dialog appears.

The onKeyDown has now been dealt with so you return true.

Your dialog is now showing,

when you press yes you want to imitate the back button which is what finish(); does

when you press no you just dismiss the dialog and the activity continues

You'll want this:

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event)  
{    
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
    { 

        alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Tile");
        alertDialog.setMessage("data lost, are you sure?");

        alertDialog.setButton(-1, getString(R.string.yes), new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                finish(); // or yourContext.finish();
                //I only can return without a boolean value here.                   
            }
        });

        alertDialog.setButton(-2, getString(R.string.no), new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                // do nothing dialog will dismiss
            }
        });

        alertDialog.show();
        return true; //meaning you've dealt with the keyevent
    }   
    return super.onKeyDown(keyCode, event);
}



回答2:


alertDialog.show(); should be called within onClick of No button



来源:https://stackoverflow.com/questions/6289718/android-intercepting-back-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!