Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullify listener in Fragment onDetach()?

Usually in my fragments I attach a listener in onAttach() and nullify the listener in onDetach().

Is setting the listener to null in onDetach() necessary?

Although I do it because it makes the code look more symmetric, it doesn't seem necessary since the fragment is already destroyed because onDestroyView() and onDestroy() were already called before according to the fragment's lifecycle.

Thanks in advance.

like image 900
Henrique Rocha Avatar asked Nov 10 '22 02:11

Henrique Rocha


1 Answers

It makes sense if you want to notify the listener of an finished AsyncTask but you are not interested in the result if the Fragment is not attached anymore. In the onPostExecute you then check if the listener is still present and if so use it.

So yes, there is at least one use cases where it makes sense to set the listener to null.

like image 187
Kuno Avatar answered Nov 15 '22 04:11

Kuno