1

Hi i want to know how to close an application in Android. Actually i am having idea by using the finish() method we can close present activity.. But what i want is, the following code defines remaining...

Main.java

   Handler handle=new Handler();

    handle.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            startActivity(new  Intent(ZzzzzProjActivity.this,Second.class));
        }
    }, 3000);

Second.java

/*** same above code***/

Third.java

   @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        Toast.makeText(getApplicationContext(), "backbutton", 30).show();
        finish();

    }
    return super.onKeyDown(keyCode, event);

}

As per the following code after coming to Third.java, when i click back button it is navigating back to Second.java page. But what i want is my applications needs to close totally. Can anyone help me with this....

MBMJ
  • 5,323
  • 8
  • 32
  • 51
code_finder
  • 1,370
  • 2
  • 21
  • 39

4 Answers4

5

you can create a dialog activity asking if the user wants to exit. in the intent you can set

Intent.FLAG_ACTIVITY_CLEAR_TOP

It will clear everything the stack and close you app. or you can do what @user1208720 has suggested.

drulabs
  • 3,071
  • 28
  • 35
1

for Achive that you should finish the current Activity when you call another Activity from Current one, like when you call second.class from first one you should finish first one and so on...,

i.e when you call another activity.class then you should finish the currentActivity.class by

like

CurrentActivity.this.finish();

user1208720
  • 515
  • 2
  • 6
  • this one is related to finish() method only. it goes back to previous activity. but what i want is need to close application totally... – code_finder May 28 '12 at 07:39
  • what @user1208720 meant is before jumping to next activity close the current activity. – drulabs May 28 '12 at 07:44
  • thanks dude got it what i am thinking about....... but can we follow this CurrentActivity.this.finish(); method in every application we create. there wont be any problem by using this method.. write? – code_finder May 28 '12 at 07:47
  • yes , you should , but mostly it depends on your application requirement. – user1208720 May 28 '12 at 08:01
1

Use that code to finish your application

Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(startMain);
Naresh Sharma
  • 4,323
  • 7
  • 48
  • 68
0

Finish each activity as you are leaving it. Like this whenever you click back, there are no previous activities so the application will exit.

Another way (that is usually not recommended for Android) is to use "System.exit(0);"

Kalaji
  • 425
  • 1
  • 4
  • 13