code_finder
code_finder

Reputation: 1370

Closing an android application ?

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....

Upvotes: 1

Views: 275

Answers (4)

Naresh Sharma
Naresh Sharma

Reputation: 4323

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

Upvotes: 1

Kalaji
Kalaji

Reputation: 425

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);"

Upvotes: 0

drulabs
drulabs

Reputation: 3121

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.

Upvotes: 5

user1208720
user1208720

Reputation: 515

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

Upvotes: 1

Related Questions