You can say they are similar:
-
The onDestroy()
method is called when the Activity is finished and is performed before destroying the Activity
, either because the finish()
method is called or because the operating system requires memory.
-
The finalize()
method is called when the object is finalized by the GC.
They are similar because: in both methods you can perform actions before the Activity or the Object is destroyed.
The difference is: finalize () is called by the garbage collector (Gargage Collector) while onDestroy () is called when the method finish ()
is executed or because the system is temporarily destroying this instance of the activity to save memory. **
From the documentation:
finalize () Call by garbage collector (Gargage Collector) of an object when the
collection of unused items determines that there is no more
references to the object.
onDestroy () The last call you receive before the activity be
destroyed. 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
memory. You can distinguish these two scenarios with the method
isFinishing ().
The finalize()
method for an Activity exists, but remember something important, do not try to be smarter than the Garbage Collector, since he will decide when to remove the instance. I mention it because it was previously common see this call to try to free memory ⤜ (ʘ_ʘ) ⤏, also its execution is determined by the Garbage Collector.