Search code examples
javaandroidandroid-activity

Calling finishAffinity() does not destroy android app or activity. Activity's data still persists even when app is restarted


This is a huge Android Programming issue/bug.

Calling finishAffinity() does not shut down my application.
This is evidenced from the fact that:

1. The Android Studio Debugger still says 'Application is running' even after the app disappears from the screen and I'm back to the phones home screen.

2. When 'restarting' the application the values of data members of the inital Activity remain the same as they were before the application supposedly 'shutdown'.

3. If I call System.exit(0) directly after finishAffinity() then the shutdown works properly and data members are reset to their initial default values.


There are even bigger implications to this problem! When I call finish() within ActivityA after starting another ActivityB, if I ever go back to ActivityA then the data members have not been reset to default are still the old values.

This is ALL confusing to me since I've come from C++ where when a class is destroyed then it is ACTUALLY destroyed and all memory associated with it is completely released.

Getting an activity to completely delete itself when switching to a new activity or trying to exit the application seems IMPOSSIBLE.

public class Startup extends AppCompatActivity
{
    private static int iStarted = 0;

    ............

    @Override
    protected void onActivityResult(int request, int result, Intent data)
    {
        super.onActivityResult(request, result, data);

        if (request == RESULT_EULA_RETURNED)
        {
            // shutdown
            finishAffinity(); // iStarted remains = 1
            return;
        }
    }

    ..........

    @Override
    protected void onResume()
    {
        super.onResume();

        // perform startup
        // even when restarted this remains  = 1
        if (iStarted == 0)
        {
            iStarted = 1; // this will stay = 1 until the application is manually killed via the CLOSE ALL method or via the debugger
        }
    }
}

Solution

  • finishAffinity() is not used to "shutdown an application". It is used to remove a number of Activitys belonging to a specific application from the current task (which may contain Activitys belonging to multiple applications).

    Even if you finish all of the Activitys in your application, the OS process hosting your app does not automatically go away (as it does when you call System.exit()). Android will eventually kill your process when it gets around to it. You have no control over this (and that is intentional).

    If you have a debugger attached to the process, this can also prevent the process being killed by Android, as the debugger keeps active objects in the process.

    You talk about "data members" not being cleaned up, and you claim that this works differently in C++. Actually, that's not true. Your "data members" are declared static. They aren't instance variables, they are class variables. They exist only once (not in every instance of the class), they are created and initialized when the class is loaded, and they are never destroyed until the class is unloaded (which never happens on Android). C++ has exactly the same behaviour.

    You might try using instance variables instead of class variables to solve your problem.