Q:

What to do when context is null in fragment

Is it safe (correct) to simply return from method's and do nothing if context is null? currently I'm doing this when ever I need context from fragment.
Context context = getContext();
if(context == null) return;

My question is too stupid and obvious but I'm still not sure if this is correct. if its necessary or not. (maybe I should log warnings?)

I do this check in onCreateView, onViewCreated, when getting context from itemView in view holders and inside view click listeners when ever needed.

A:

I think retrunning and doing nothing is as bad as catching an exception and swallowing it.
Fragment has a method requireContext(). It simply crashes the app when the context is null. Actually, I haven't never seen my app is crashed because of it. So I guess a null context in a fragment is a pretty rare and extreme case.
In the onViewCreated, you can also get context from the created view. It's non null because the view creation requires a context.
If you need some resources, you can also use getResources().getString() instead of getContext().getString()

