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 8094655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:51:46+00:00 2026-06-05T20:51:46+00:00

I have an application in which I have different activities. In 1 activity, I

  • 0

I have an application in which I have different activities. In 1 activity, I want that when the user presses the back button, I want the application to be closed and home screen is displayed

Code

public boolean onKeyDown(int keyCode, KeyEvent event) 
        {
            //Handle the back button
            if(keyCode == KeyEvent.KEYCODE_BACK) 
                {
                    //Ask the user if they want to quit
                    new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle("Exit")
                    .setMessage("Really Exit ?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
                        {
                            public void onClick(DialogInterface dialog, int which) 
                                {
                                 Intent intent = new Intent(Intent.ACTION_MAIN);
                                intent.addCategory(Intent.CATEGORY_HOME);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                                }
                        })
                    .setNegativeButton("No", null)
                    .show();
                    return true;
                }
            else 
                {
                return super.onKeyDown(keyCode, event);
                }
        }

When I run this and press the back button, the home screen is displayed, but when I run it for the second time, I get a console output as

ActivityManager: Warning: Activity not started, its current task has been brought to the front.

And the activity in which the back button is pressed gets displayed.
I think the application does not get killed and runs in background. Just to mention, this is not the starting activity of my application.

Can somebody help me, I am beginner.

  • 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-06-05T20:51:48+00:00Added an answer on June 5, 2026 at 8:51 pm

    For each and every Intent you have used for going into other activity u have to follow this way for passing intent just pass flag to each intent as given below and after starting Activity using startActivity() u have to add finish() after that demo code as given below

    Intent i=new Intent(firstActivity.this,secondActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For instance, you have an application which processes files that are sent by different
I have an application which needs to run with different user name. I am
I have created an application having tab activity which in turn has 5 different
I have one Application which has more then 5 Activity I want to know
I have an application which has 11 different activities. One of these activities is
I have a python (pygtk) application which starts in different modes depending on arguments.
I have a web application which process the data collected from different client machines
I have a application executable, which runs with different parameters to produce different outputs.
I have a multithreaded application, in which I'm trying to render with different threads.
My application has several independent top-level windows, which all have completely different functions/workflows. I

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.