Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
        }
    }
}
like image 382
zdanman Avatar asked Sep 19 '16 22:09

zdanman


2 Answers

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.

like image 181
David Wasser Avatar answered Oct 15 '22 11:10

David Wasser


Android has no concept of "shut down my application". There is only the Android Activity life cycle. There is not a connection between the VM's object life cycle on the activity life cycle; Android is free to re-use your Activity object instance across on create / destroy boundaries. In short, you can't assume Android will ever construct a new Activity object to handle a life cycle event.

You'll need to manage your own state. For example, maybe you want to clear them in onCreate() so whenever you activity is re-created they are reset. Note that I don't presume to know what logic you want to apply to the problem, I'm just giving an example.

like image 45
Jeffrey Blattman Avatar answered Oct 15 '22 11:10

Jeffrey Blattman