当返回按钮是pressed提示用户按钮、提示、用户、pressed

2023-09-12 22:18:32 作者:过期关系.

这是要求用户的好地方,无论他(她)要退出该应用程序时,他(她)单击后退按钮?我认为的onPause 的onStop ,但每当应用程序是走了后面的其他应用这些方法火灾。

更新: 该应用程序还应该问,如果用户试图退出从按钮的应用程序(在应用程序本身),而不是回到硬键。

解决方案   

询问用户的权限关闭   应用程序。

您可以做到这一点通过以下方式;

  / **
 *退出应用程序,如果用户选择yes。
 * /
私人无效doExit(){

    AlertDialog.Builder alertDialog =新AlertDialog.Builder(
            AppDemoActivity.this);

    alertDialog.setPositiveButton(是,新OnClickListener(){

        @覆盖
        公共无效的onClick(DialogInterface对话,诠释它){
            完();
        }
    });

    alertDialog.setNegativeButton(否,NULL);

    alertDialog.setMessage(是否要退出?);
    alertDialog.setTitle(AppTitle);
    alertDialog.show();
}
 

  

这是要求用户的好地方,   是否他(她)想要退出该应用   当他(她)单击后退按钮?

由于,要提示用户,当他(她)点击后面的硬键,所建议的人,你可以覆盖 onBack pressed()方法,并从那里调用上述方法;

  @覆盖
公共无效onBack pressed(){

    doExit();
}
 
按钮设计的7条基本规则

所建议的@Martyn,您可以使用的onkeydown 来实现相同的;

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

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

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

  

应用程序还应该询问用户是否是   试图退出从按钮的应用程序   (在应用程序本身),而不是回到硬   关键的。

对于这一点,叫 doExit()从按钮的onClick;

 按钮exitButton =(按钮)findViewById(R.id.exitButton);
exitButton.setOnClickListener(新android.view.View.OnClickListener(){

    @覆盖
    公共无效的onClick(视图v){
        doExit();
    }
});
 

Which is the good place to ask user, whether (s)he wants to exit the app when (s)he clicked the back button? I considered the onPause and onStop, but these methods fires whenever app is gone behind the other apps.

Update: The app should also ask if user is trying to exit the app from a button (in app itself), not the Back hard key.

解决方案

Ask for user's permission to close the app.

You can do it in the following way;

/**
 * Exit the app if user select yes.
 */
private void doExit() {

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            AppDemoActivity.this);

    alertDialog.setPositiveButton("Yes", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });

    alertDialog.setNegativeButton("No", null);

    alertDialog.setMessage("Do you want to exit?");
    alertDialog.setTitle("AppTitle");
    alertDialog.show();
}

Which is the good place to ask user, whether (s)he wants to exit the app when (s)he clicked the back button?

Since, you want to prompt user when (s)he clicked the back hard button, as suggested by others, you can override the onBackPressed() method and call the above method from there;

@Override
public void onBackPressed() {

    doExit();
}

As suggested by @Martyn, you can use the onKeyDown to achieve the same;

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

    if(keyCode == KeyEvent.KEYCODE_BACK) {
        doExit();
    }

    return super.onKeyDown(keyCode, event);
}

The app should also ask if user is trying to exit the app from a button (in app itself), not the Back hard key.

For this, call the doExit() from your button's onClick;

Button exitButton = (Button)findViewById(R.id.exitButton);
exitButton.setOnClickListener(new android.view.View.OnClickListener() {

    @Override
    public void onClick(View v) {
        doExit();
    }
});