Prompt user when Back button is pressed

前端 未结 5 1008
孤街浪徒
孤街浪徒 2021-02-13 16:01

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 t

相关标签:
5条回答
  • 2021-02-13 16:02

    look at this link..

    You should not exit your application.

    Is quitting an application frowned upon?

    0 讨论(0)
  • 2021-02-13 16:03

    This is a rather old question but it ranks high on Google so I figured it could need an Update:

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Do you want to exit the app?");
    
        // if user presses "Exit", then he is allowed to exit from application
        builder.setPositiveButton("Exit", (dialog, which) -> finish());
    
        // if user selects "Stay", just cancel this dialog and continue with app
        builder.setNegativeButton("Stay", (dialog, which) -> dialog.cancel());
    
        builder.create().show();
    }
    
    0 讨论(0)
  • 2021-02-13 16:12

    The best place is in the onBackPressed. the only thing you need to keep in mind is that the app behaves 'normally' - that is when the user pressed back they should go to the previous screen - if they press back on the main screen, then it's good to ask if they meant to quit the app.

    0 讨论(0)
  • 2021-02-13 16:23

    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();
        }
    });
    


    Related Info:

    • Implementing effective navigation
    0 讨论(0)
  • 2021-02-13 16:25

    If it's only when the user presses back (not home or whatever), use onBackPressed.

    The doc says:

    Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

    So if you catch the backpress and don't do anything, the activity is not quit. If you want to quit, use finish()

    0 讨论(0)
提交回复
热议问题