exit android application programmatically

孤人 提交于 2019-11-28 06:37:39
Sripathi

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

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>

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"

This is the cleanest way I have come across:

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