Good morning,
When an activity is destroyed on Destroy what happens with the classes that were instantiated in this activity, are they also destroyed or are they loaded in memory?
Good morning,
When an activity is destroyed on Destroy what happens with the classes that were instantiated in this activity, are they also destroyed or are they loaded in memory?
onDestroy () Perform any cleanup final before destroying an activity. This can happen either because the activity is ending (someone called finish () or because the system is temporarily destroying this instance of the activity to save space. You can distinguish these two scenarios with the method isFinishing ().
The onDestroy () method is actually a method called by the system before the activity is destroyed, which we can observe in the life cycle diagram of Activity
.
Executing onDestroy () occurs for two situations:
finish()
) According to your question:
when an activity is destroyed onDestroy what happens with the classes that were instantiated in this activity, are they also destroyed or remain loaded in memory?
I guess you want to know what happens with classes that you've installed, well at this point to be called onDestroy () tells us that the Activity
is about to be destroyed and the resources released, in this case the instances of Classes will be destroyed.
It is important to know that the Garbage Collector does not release these instances instantly.
It depends, what type of variable you declare. If it is a variable static
this is typical of the class and they are loaded (forgive the redundancy) when the class is loaded and not when it is instantiated. On the other hand, if within the class (or the onCreate
method in this case) you declare variables are alive and die within the life cycle of your Activity
, a Activity
has a unique instance strong> and there can not be two different instances.
When the Activity
is "Stopped" it goes into the background, since the Activity
main is another, it is HERE where it keeps all its data as it was, all in memory. But the system is smart enough to eliminate it if you need memory space because it is not attached to the window manager.
Regarding the method onDestroy()
is a method that is executed BEFORE being completed the Activity
I say before, because to execute this method someone or something should have called finish()
or finishActivity()
or because the system is releasing memory you need.
Regarding the correct thing, when a Activity
goes to the background, it is the method onPause()
which corresponds to handle the data that until now, your Activity
maintains, this is where you should know what to do with those data in the case that for reasons of memory are released, important data and that only affect this Activity
, for example the camera, animations, etc. that are occupied only in this instance of your activity.
As a first clarification, the activity is not destroyed in OnDestroy (). OnDestroy is a method called by the system before the activity is destroyed, this method is for you to clean what you have to clean.
The destruction of an activity does not directly imply that the GC (garbage collector) is called about the activity, but if the system needs memory it will do so at any time, therefore you need to save all the data that you do not want to lose.