If you are running code that gets null
for getActivity()
or getContext()
for that matter, that means that your code is running after your fragment has been detached, onDetach()
was called.
This happens probably because you were running an async operation, and when the result came back your fragment was detached from the activity, or it might even have been destroyed. So in this case it is valid to find getActivity
to return null
, and in this case you just need to either:
- Do nothing, because you wanted to update the fragment's view, but that's not shown to the user anymore.
- Save the data you or the state you got so you can re-use it when/if
the fragment is re-attached again.