Close application and remove from recent apps/
original source : https://stackoverflow.com/questions/22166282/close-application-and-remove-from-recent-apps
I based my solution on guest’s above, as well as gilsaints88’s comments below (for Android L compatibility):
Add this activity to your AndroidManifest.xml file:
<activity
android:name="com.example.ExitActivity"
android:theme="@android:style/Theme.NoDisplay"
android:autoRemoveFromRecents="true"/>
Then create a class ExitActivity.java:
public class ExitActivity extends Activity
{
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(android.os.Build.VERSION.SDK_INT >= 21)
{
finishAndRemoveTask();
}
else
{
finish();
}
}
public static void exitApplication(Context context)
{
Intent intent = new Intent(context, ExitActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(intent);
}
}
Then whenever you want to force close your application and remove it from the recent’s list, call:
ExitActivity.exitApplication(context);
This solution works for me instead of declaring android:excludeFromRecents=“true” because I want the user to be able to see the app in the recents list UNTIL the point where my app triggers closing the app programmatically.
여러 방법이 있지만 위의 방법이 완전히 app도 close되고 recent app (home 버튼 클릭시 나오는 화면)에도 흔적을 남기지 않았다.
참고사항
https://stackoverflow.com/a/29643900/3151712
For exiting app ways:
Way 1 :
call finish();
and override onDestroy();
. Put the following code in onDestroy()
:
System.runFinalizersOnExit(true)
(위의 방법은 runFinalizersOnExit()은 deprecated되었다.)
or
android.os.Process.killProcess(android.os.Process.myPid());
Way 2 : (이방법은 기본 activity로 이동하고 recent app에도 흔적이 남았다.)
public void quit() {
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
System.exit(0);
}
Way 3 : (현재 하나의 activity만 종료된다.)
Quit();
protected void Quit() {
super.finish();
}
Way 4 :
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Way 5 :
Sometimes calling finish()
will only exit the current activity, not the entire application. However, there is a workaround for this. Every time you start an activity
, start it using startActivityForResult()
. When you want to close the entire app, you can do something like the following:
setResult(RESULT_CLOSE_ALL);
finish();
Then define every activity’s onActivityResult(...)
callback so when an activity
returns with the RESULT_CLOSE_ALL
value, it also calls finish()
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode){
case RESULT_CLOSE_ALL:{
setResult(RESULT_CLOSE_ALL);
finish();
}
}
super.onActivityResult(requestCode, resultCode, data);
https://stackoverflow.com/a/32659591/3151712
Try this on a call. I sometimes use it in onClick of a button.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
It instead of closing your app , opens the dashboard so kind of looks like your app is closed.
read this question for more clearity android – exit application code
이방법을 통해 app을 종료할수 있었으나 recent app에 흔적을 지울수는 없었다.