Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

lint

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?

like image 736
Trevor Avatar asked Dec 27 '17 15:12

Trevor


3 Answers

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.

like image 194
Vasiliy Avatar answered Nov 16 '22 23:11

Vasiliy


These methods was added in Revision 27.1.0 Release:Fragments now have requireContext(), requireActivity(), requireHost(), and requireFragmentManager() methods, which return a NonNull object of the equivalent get methods or throw an IllegalStateException.

like image 6
Mohammad Mirzakhani Avatar answered Nov 17 '22 00:11

Mohammad Mirzakhani


This is a duplicate of - Android Studio 3.0 lint warnings for references to activity.

tldr; getActivity() got with Support lib 27.0.0 the annotation @Nullable and static analyze tools pick that up now.

like image 3
Niklas Avatar answered Nov 17 '22 00:11

Niklas