Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6024713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:11:45+00:00 2026-05-23T04:11:45+00:00

Which is the good place to ask user, whether (s)he wants to exit the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:11:45+00:00Added an answer on May 23, 2026 at 4:11 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if this is a good place to ask questions like this,
I've a WPF+WCF application which works good for now 2 years, but now they
I know there are many cases which are good cases to use multi-thread in
Short story: stopPropagation() prevents a dropdown menu from closing - which is good. But
The code I used doesn't give me any MySQL errors (which is good) Except
I set my embedded bespin as below, which works good: _editorComponent = new bespin.editor.Component('editor',
Which is a good Project Managment Method to Develop a Website? XP? The Waterfall
Which is a good architecture for a Web Application using OSGI and GWT as
Is there any good library which supports WebSockets and is compatible with GWT? Ideally,
While running my application (which does a good bit of custom drawing) I get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.