jennifer
jennifer

Reputation: 8261

How to finish an android application?

I need to finish an android application. For that i wrote

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure You want to exit")
        .setCancelable(false)
        .setPositiveButton("YES"),
        new DialogInterface.OnClickListener() {
            // On
            // clicking
            // "Yes"
            // button

            public void onClick(DialogInterface dialog,int id) {
                System.out.println(" onClick ");
                closeApplication(); // Close Application method called
            }
        })
        .setNegativeButton("NO"),
        new DialogInterface.OnClickListener() {
            // On
            // clicking
            // "No"
            // button
            public void onClick(DialogInterface dialog,int id) {
                dialog.cancel();
            }
        });

        AlertDialog alert = builder.create();
        alert.show();
    }

    private void closeApplication() {
        System.out.println("closeApplication ");
        this.finish();
    }
}

But if any activity is not finished in the application, when i tried to exit the application that activity is finished first and the application is not exiting.. i tried 2 times to exit this application... How i can finish all the activities in an application when i need to exit... or is there any way to finish the entire application

Upvotes: 35

Views: 83069

Answers (9)

Hossein Yousefpour
Hossein Yousefpour

Reputation: 4953

according to this answer,

just write this.finishAffinity(); and done!

Upvotes: 3

Stan Malcolm
Stan Malcolm

Reputation: 2790

Put this into your onClick or in onBackPressed:

moveTaskToBack(true);
finish()

Upvotes: 21

TheIT
TheIT

Reputation: 12219

Shameless copy of NeTeInStEiN's answer as I found it so useful (please up-vote his answer): Sending a running application to background programmatically

You can use either:

boolean sentAppToBackground = moveTaskToBack(true);

if(!sentAppToBackground){
  Intent i = new Intent();
  i.setAction(Intent.ACTION_MAIN);
  i.addCategory(Intent.CATEGORY_HOME);
  this.startActivity(i);
}

More information here: http://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)

Or simply:

Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);

According to Romain Guy a Android Framework Engineer, "You cannot simulate a press on the Home key.". So beware...

Check: http://osdir.com/ml/Android-Developers/2010-03/msg01887.html

Updated this answer according to: moveTaskToBack(true) returns false always

Upvotes: 1

redestructa
redestructa

Reputation: 1202

To Close the Application, you can also take "System.exit(0)" 0 is standard or use any exit code.

Upvotes: -3

inquist
inquist

Reputation: 197

I have an application with several Activities. I extended my Application class, and included a variable numActive. This is initialized to 0. Within each activity's onStart(), numActive is incremented, and in onStop() it is decremented. If the count reaches zero, the user has left my application entirely, and I close down my background tasks.

Upvotes: 1

Kevin Gaudin
Kevin Gaudin

Reputation: 9945

Please read first this post from Google Android Developer Advocate Reto Meier : When to Include an Exit Button in Android Apps (Hint: Never)

What is th symptom that make you want to add an exit button ? If you need to clear the activity stack and always restart with a specific Activity, maybe you just have to tweak your activity manifest declaration with attributes like : android:clearTaskOnLaunch

Upvotes: 13

Barmaley
Barmaley

Reputation: 16363

To close application just call:

android.os.Process.killProcess(android.os.Process.myPid());

Otherwise due-to specific life-cycling of Android activities you can't be sure that application is closed/killed.

Upvotes: 56

Aman Aalam
Aman Aalam

Reputation: 11251

Android is made in such a way that virtually NO application that was once opened, is closed.

Before mis-interpreting the statement, understand this.

"Whenever you exit your app, Android saves all the things the app was doing (called its state) and pushes the app in the background, calling the onStop() method. this is the new state of the application then, where the app isn't running, but isn't flushed out of the memory too. whenever you start the app again, it is resumed from the frozen state. Only when the memory, where frozen apps are kept, starts getting full, the Android GC flushes the app."

So conceptually, nothing goes out. when you hit "back" button while ur on the first activity, Android bundles the app and data, and freezes it.

Upvotes: 10

Pinki
Pinki

Reputation: 21919

whenever you are starting a new activity use

myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myintent);

and in manifest file mention that activity as

<activity android:name=".<Activity Name>" >
        <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
     <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
       </activity>

Upvotes: 28

Related Questions