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?

4
  • Android kill process own its own, it is not a good practice to kill Processes on your own. finish() is the best way to exit from an activity Commented Aug 7, 2015 at 7:12
  • ok, I used Finish(); alone , Does not work. I used Finish() with System.exit(0), Does not work. I tried many ways, I did not answer. Commented Aug 7, 2015 at 7:15
  • check this. stackoverflow.com/questions/15202259/… Commented Aug 7, 2015 at 7:15
  • finish() finishes an activity, if your app contains multiple activity, you need to close all of them Commented Aug 7, 2015 at 7:16

3 Answers 3

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

excuse me , your Idea dose not work. my problem is still there.
thanks, my problem was solved. I used 'android:excludeFromRecents="true" ' in the first activity tag ( in manifest).
excuse me , my reputation is lower than 15.
0

finish() is enough.

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.