Search code examples
androidandroid-layoutandroid-activity

Exiting an application completely keeps activity in background - how to exit completely?


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!


Solution

  • 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.