Calling finishAffinity() does not destroy android app or activity. Activity's data still persists even when app is restarted
finishAffinity()
is not used to "shutdown an application". It is used to remove a number of Activity
s belonging to a specific application from the current task (which may contain Activity
s belonging to multiple applications).
Even if you finish all of the Activity
s 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.
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.