DialogFragment getActivity() "might be null" lint warning in AndroidStudio 3.0.1

Trevor :

The closest existing question I can find regarding this is Android Studio 3.0 lint warnings for references to activity, but it does not help.

Using AndroidStudio 3.0.1, I have a DialogFragment where I do this usual stuff:

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        ...

I have a lint warning moaning at me that Argument 'getActivity()' might be null.

I understand why getActivity() may be null, and I understand how the lint inspection knows this (from the @Nullable annotation).

My question is: It's all very well and good that getActivity() may be null, but practically how am I supposed to deal with this gracefully and tidily? onCreateDialog must return a Dialog (because of the superclass' @Nullable annotation) so I must have the Activity context to create it.

I could assume that onCreateDialog will never be called if the DialogFragment isn't attached to an Activity, but still - how do I address the untidy lint warning?

Vasiliy :

The answer by @Niklas explains why you get this warning now. I would like to share my thoughts about what you should actually do.

First of all, all this added nullability does is exposing the old design deficiency that was present all these years - this method could always return null (e.g. Fragment detached).

I would prefer if they annotated the return value as @NonNull and throw exception internally if this method is called when the Activity is actually null, but I understand that it would break backward compatibility and as such very risky (though I can hardly see why would anyone call this method when the Activity can actually be null).

So, what should we do about it?

First of all, since the functionality didn't change at all, if the code in question already worked then do what @CommonsWare suggested - either supress the warning or ignore it.

You could also wrap each call into null check with e.g. exception.

What I'm going to do, however, is to put this method in my BaseDialog (which is extended by all other dialogs):

protected FragmentActivity getActivityNonNull() {
    if (super.getActivity() != null) {
        return super.getActivity();
    } else {
        throw new RuntimeException("null returned from getActivity()");
    }
}

Note that all these options effectively state that you don't really expect null to be returned and is OK with app crashing if that happens. That's why I said that I would prefer to have this in support library code instead.

Edit:

A new method was added to support Fragments - requireActivity(). This method is equivalent to getActivityNonNull() descried above (though it throws IllegalStateException if not attached to Activity).

Use this method instead of getActivity() and you should be good.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=420684&siteId=1