问题
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);
}
回答1:
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
回答2:
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>
回答3:
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"
回答4:
This is the cleanest way I have come across:
moveTaskToBack(true);
finish();
来源:https://stackoverflow.com/questions/23703778/exit-android-application-programmatically