Reset listener in onDetach () fragment?

Usually in my snippets I add a listener in onAttach () and nullify the listener in onDetach ().

Does the listener set null to onDetach () necessary?

Although I do this because it makes the code more symmetrical, it does not seem necessary, because the fragment is already destroyed, because onDestroyView () and onDestroy () were already called earlier in accordance with the life cycle of the fragment.

Thanks in advance.

+5
source share
1 answer

It makes sense if you want to notify the listener of the completed AsyncTask, but you are not interested in the results if Fragment is no longer bound. In onPostExecute, you then check if the listener is present and if it uses it.

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

+1
source

Source: https://habr.com/ru/post/1201144/


All Articles