onDestroy is not called for api 22

164 Views Asked by At

I am using the androidx.navigation to navigate around the fragments. I am seeing different behavior across different versions of FOS.
When i switch between the fragments onDestroy

public void onDestroy() {}

is not called for api 21 and 22 right away. It gets called only when i try to create the same fragment again.

For api > 24, same method is called right away.

Whats the different? why this behavior, I have to do some cleanup which i am doing in onDestroy. Should i use the onDestroyView instead ?

1

There are 1 best solutions below

1
On

Apparently, onDestroyView is recommended for that purpose:

onDestroyView() allows the fragment to clean up resources associated with its View.

Source: https://developer.android.com/reference/android/app/Fragment#Lifecycle