How to finish an android application?

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>

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


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.


Put this into your onClick or in onBackPressed:

moveTaskToBack(true);
finish()

Tags:

Android