Should we always nullcheck Context inside a fragment?

I have a very simple on click listener in my fragment:

button?.setOnClickListener {
    val intent = Intent(MyActivity.createIntent(context!!)) // crash here because context is null
    startActivity(intent)
}

Crashlytics shows that some users are getting KotlinNullPointerException crashes when clicking on this specific button. Now I know the problem is happening because I am force unwrapping the Context. If I simply wrapped it inside a nullcheck, it would not crash.

But I assume that there's a bigger underlying issue in my code because I always force unwrap context when I need it and I only have issues with this specific piece of code.

What's the rule here? Should we always nullcheck our Context or not?

If you will look at the source code of fragment.getContext() method, you will see:

@Nullable
public Context getContext() {
    return mHost == null ? null : mHost.getContext();
}

Which means that getContext can return null. Internally mHost represents an Activity fragment is attached to. Fragment isn't always attached to it's hosting activity, you can observe this using onAttach / onDetach lifecycle callbacks.

In your case, as already mentioned, best approach would be to use context from a View

view.setOnClickListener { it.context }

But in general, always check nullable things, and don't do !! even if you're sure it is not null. In such way you will have less error prone code, providing an alternative way of handling nulls.

Should we always nullcheck Context inside a fragment?, If I simply wrapped it inside a nullcheck, it would not crash. But I assume that there's a bigger underlying issue in my code because I always force unwrap context� Fragment isn't always attached to it's hosting activity, you can observe this using onAttach / onDetach lifecycle callbacks. In your case, as already mentioned, best approach would be to use context from a View. view.setOnClickListener { it.context } But in general, always check nullable things, and don't do !! even if you're sure it is not null.

Inside button click you can easily use : view.getContext() to get the context or in Kotlin

it.context // which will be never null 

i.e,

button?.setOnClickListener {
   val intent = Intent(MyActivity.createIntent(it.context)) // this wont ever crash
   startActivity(intent)
}

Fragment, Return the Context this fragment is currently associated with. Returns the Transition that will be used to move Views in to the scene when returning due to if the FragmentManager is null. Check if isAdded() returns false to determine if the� Always use the getActivity () method to get the context of your attached activity, but always remember one thing: Fragments are slightly unstable and getActivity returns null some times, so for that, always check the isAdded () method of fragment before getting context by getActivity (). Answer 4

most propably you will be okay with code like that

button?.setOnClickListener {startActivity(MyActivity.createIntent(it.context))}

I believe on your MyActivity.createIntent() fun you return Intent

getContext() may be null and other fun lint warnings AS loves to spit , @Override public void onAttach(Context context) { super. So my question to all of you experienced people: should I just ignore the lint warnings AS spits The reason getActivity() and getContext() can return null in a fragment is that Fragments don't need to be attached to Activities. Just do the null check on getActivity(). The logic of going to PHP i put it inside the onCreate() method of the fragment. But what i could see is that my hashmap is not ready (or with data) when the fragment starts. It seems is slower and the onCreateView() runs faster than the asyncronic method, so when tries to use the hashmap it said, hey! this variable is null.

Converted Activity to Fragment and now my code is littered with , And honestly, I haven't seen a context/activity NPE in years. Otherwise, you need to null check, or move the calls to locations you know it will be not null. 3. A default View can be returned by calling Fragment (int) in your constructor. Otherwise, this method returns null. It is recommended to only inflate the layout in this method and move logic that operates on the returned View to onViewCreated (View, Bundle).

Fragment: getContext vs requireContext | by Ivan Shafran, TLDR: use requireContext only when you are sure fragment is In Java: Context context = getContext(); if (context == null) { // Handle null� using only “this” because the Activity class extends the Context class; But you can’t use any of these two methods inside a Fragment and you have to replace them respectively with: getActivity().getApplicationContext() getActivity() I often do this replacement when I use code written for an Activity, but that I must use in a Fragment.

Fragments have tricky lifecycle and when a fragment gets detached and re-attached it is not always actually destroyed, for example, retained fragments are not destroyed during configuration changes. When this happens, the fragment’s instance survives and only its view gets destroyed, so onDestroy() is not called and DESTROYED state is not