When is onDestroy what happens with the classes instantiated in the activity?

3

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?

    
asked by FuriosoJack 13.12.2016 в 03:48
source

3 answers

1
  

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:

  • a) Because the activity has been removed from the code itself (calling finish() )
  • b) Because the operating system requires memory and is trying to free up memory for use in other applications or the system itself.

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.

    
answered by 13.12.2016 / 20:22
source
2

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.

    
answered by 13.12.2016 в 12:30
1

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.

    
answered by 13.12.2016 в 11:16