2

I have a HomeActivity (for show splash screen in 3 seconds) , then automatically redirect to LoginActivity (for check users information for login). In LoginActivity I have a exit button for exit the app, with below code

        // TODO Auto-generated method stub
         finish();
         android.os.Process.killProcess(android.os.Process.  myPid());
         System.exit(0);

I used the same code in onDestroy() again. But , when I try to exit from the app , Program is firmly closed. but remains in memory (in background app list ). How can I solve it?

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82

3 Answers3

3

It's not a good idea to call:

android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);

because android will handle processes automatically. Also exclude your app from recent isn't a correct behavior.

By the way you can put under your "exit" activity tag in manifest:

android:excludeFromRecents="true" 

And it will not appear in recent apps when the app is closed

EDIT

If it doesn't work in Android 5.0 it was a reported bug, so add taskAffinity property and use autoRemoveFromRecents:

android:taskAffinity=".YourExitActivity"
android:autoRemoveFromRecents="true"

Then in your onPause() you can check the sdk version to use finishAndRemoveTask:

if(android.os.Build.VERSION.SDK_INT >= 21) {
    finishAndRemoveTask();
} else {
    finish();
}
Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70
0

finish() is enough.

If the app still remains in memory after call finish(), it may be Memory leak in your app.

Money
  • 346
  • 2
  • 3
0

While mentioning tag in manifest we can keep

 `android:`noHistory=true

so that mentioned activity wont be in back stack.

For example:

<activity
            android:name="Splash_Activity"
            android:label="Splash"
            android:noHistory="true" />

You can call just finish() to close activity.

Amarjit
  • 4,327
  • 2
  • 34
  • 51
Jayaprakash
  • 101
  • 5