0

I want my app to exit completely, even from background.

Firstly, quick explanation of my app:

I have the following activity:

Launcher -> Main -> A -> B -> C

When I open app, I fire off the launcher, checks if I have logged in before (this is usually the case, answer is yes, I'm logged in), finish() -> move to Main screen, (answer a question in main) -> move to A (..) -> move to B (...) -> move to C

without using any finish between Main, A, B and C.

in C I have exit button, I want it to exit the app completely.

Here is my code:

Launcher:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    checkIfUserLoggedIn();
    //some other code
}

private void checkIfUserLoggedIn() {
    String codeStr = DatabaseMgr.getPreferences(getString(R.string.savedCode), this);
    if (codeStr != null && !codeStr.isEmpty()) {
        moveToMainScreen();
    }
}

private void moveToMainScreen() {
    Intent intent = new Intent(MainActivity.this, MainScreen.class);
    startActivity(intent);
    finish();
} 

from Main -> A -> B -> C ==> same code as:

Intent intent = new Intent(<ACT>.this, <Next ACT>.class);
            startActivity(intent);

pressing exit in C:

Intent intent = new Intent(getApplicationContext(), MainScreen.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("EXIT", true);
            startActivity(intent);

MainScreen Activity:

if (getIntent().getBooleanExtra("EXIT", false)) {
        //1- this.finishAffinity();
        //2- finish()
        //3- Intent i = new Intent();
        //i.setAction(Intent.ACTION_MAIN);
        //i.addCategory(Intent.CATEGORY_HOME);
        //ListActivity.this.startActivity(i); 
        //finish();   

        //4- android.os.Process.killProcess(android.os.Process.myPid());
        return;
    }

all of the above attempts leave me this on phone:

background

what am I doing wrong?

is there any other way to exit application completely ? not even keeping it as in attached picture ??

Thanks alot!

Michael
  • 79
  • 2
  • 7
AndroidDev
  • 91
  • 1
  • 1
  • 9
  • It looks like what you are showing is your app appears in the list of "Recently Use" apps. So what you are asking is how can you force the OS to remove your app from that list? – Barns May 24 '18 at 18:56
  • Related: [https://stackoverflow.com/questions/13385289/remove-app-from-recent-apps-programmatically](https://stackoverflow.com/questions/13385289/remove-app-from-recent-apps-programmatically) – Bö macht Blau May 24 '18 at 19:14
  • 1
    @AndroidDev What you see is a screenshot of the app, taken by the Android OS. It does not necessarily mean that the `Activity` is still running. – M. le Rutte May 24 '18 at 19:14
  • @Barns yes, I dont want the user to be able to see it in recent app and click on it. – AndroidDev May 24 '18 at 20:02
  • @M.leRutte, but the user still can click on it and re-open it – AndroidDev May 24 '18 at 20:02
  • Yes, if the application has been closed completely by the Android system it will start a new process and load your activity. – M. le Rutte May 24 '18 at 20:05
  • "yes, I dont want the user to be able to see it in recent app and click on it." then try my suggestion and see if it meets your expectations – Barns May 24 '18 at 20:07
  • @M.leRutte and I don't want user to re-open it from recent apps. – AndroidDev May 24 '18 at 20:08

2 Answers2

2

It appears as if you are asking how to keep your app from appearing in the list of "Recently Used" apps?

In that case, you can add a property to the app manifest. I have only tried adding this property to the "MainActivity" in my app and it worked:

android:excludeFromRecents="true"

I do not know if you must add it or all the Activity entries in the manifest file, but from my tests it is sufficient to put it in the "MainActivity".

Alternatively, you might try adding the flag:

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

to your Intent.

Barns
  • 4,640
  • 3
  • 12
  • 27
  • @AndroidDev :: if this answer solved your problem there is no crime in accepting this answer. – Barns May 24 '18 at 20:49
  • Sure man!, I have added "android:excludeFromRecents="true"" to every activity and worked! thanks a lot m8! – AndroidDev May 24 '18 at 21:00
0

EDIT:

You can call finishAndRemoveTask(); to clear it completely if your API level is >= 21.

Old Answer:

Try using:

android.os.Process.killProcess(android.os.Process.myPid());

before this code:

Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
Michael
  • 79
  • 2
  • 7