exit android application programmatically

牧云@^-^@ 提交于 2019-11-27 01:29:39

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!