Search code examples
androidandroid-fragmentsandroid-lifecyclefragmentmanagerandroid-components

Android Fragment isAdded returns false and getActivity is null after posting thread in the onResume method


So i am using Navigation in my main activity of my app and i have a fragment which is my start navigation fragment.

In this fragment, after it is created, in my presenter i post a thread to fetch data from the network. After data have been fetched, i am using the main thread to show the data to my screen.

The first time that the app runs, this is working fine.

However if user opens the drawer and selects again THIS fragment and not another one, fragment is recreated again meaning that it gets destroyed and created from scratch as Navigation Component is designed to do.

However this time, when my presenter posts thread fetching-data-thread and this gets finished and sends the results to the UI, fragment's isAdded() method returns false as well as getActivity is null.

Having that, means that i can't use the Activity Context (getActivity() is null or requireActivity() throws an illegal state exception) and consequently i cannot load images etc since i don't have the context available.

I highlight that this happens when user opens the drawer while this fragment is visible and selects again to navigate to this fragment from the drawer. In case that user navigates to another fragment and then presses the back button everything is ok.

Any idea how to handle this problem?


Solution

  • So, after testing and searching i found out the origin of the problem i described above.

    I am nullifying my presenter's view in onDestroy/onDetach method of my Fragment. However when the replacement Fragment gets created, this new Fragment is firstly attached to the calling Activity and then, the old one gets destroyed.

    Having in mind, that i inject my presenter into the Fragment instance, my presenter will be never null at the time that new Fragment gets attached and consequently, and considering that i create a new instance of my Presenter when it is null, the presenter instance that is being injected into the fragment is not aware of the new 'View' object.

    As a result, when the results reach the UI thread through the callback this view object is 'not Added'.