Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exit android application programmatically

Tags:

android

exit

I have two activities, the first is a splash activity. I would like to know how to exit the application from the second activity to the homepage. I've used this method it works BUT it takes to the launcher.

public void AppExit() 
{

    this.finish();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);


}
like image 986
choco Avatar asked May 16 '14 20:05

choco


4 Answers

This is the cleanest way I have come across:

moveTaskToBack(true);
finish();
like image 91
BinaryShrub Avatar answered Oct 25 '22 10:10

BinaryShrub


when you want to close your application, you can call

finishAffinity();

or if you want close it in background also you should write,

android:excludeFromRecents="true"

in AndroidManifest :

       <activity
        android:name="com.smart.remote.main.SplashActivity"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait" 
        android:excludeFromRecents="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
like image 30
elifekiz Avatar answered Oct 25 '22 11:10

elifekiz


Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's oncreate

if (getIntent().getExtras() != null && getIntent().getExtras().getBoolean("EXIT", false)) {
    finish();
}

and you are done....

From Finish all activities at a time

like image 30
Sripathi Avatar answered Oct 25 '22 12:10

Sripathi


To finish an activity or exit the app, try this..

public void exitApp(View v)
{

finish();

}

& use this if for whatever you select to exit the app.

android:onClick="exitApp"

like image 22
Emm Jay Avatar answered Oct 25 '22 10:10

Emm Jay