DialogFragment getActivity() "might be null" lint warning in AndroidStudio 3.0.1 : <p>The closest existing question I can find regarding this is <a href="https://stackoverflow.com/questions/47138807/android-studio-3-0-lint-warnings-for-references-to-activity/47168730#comment82957500_47168730">Android Studio 3.0 lint warnings for references to activity</a>, but it does not help.</p>

<p>Using AndroidStudio 3.0.1, I have a <code>DialogFragment</code> where I do this usual stuff:</p>

<pre><code>    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        ...
</code></pre>

<p>I have a lint warning moaning at me that <code>Argument 'getActivity()' might be null</code>. </p>

<p>I understand <em>why</em> <code>getActivity()</code> may be null, and I understand how the lint inspection knows this (from the <code>@Nullable</code> annotation). </p>

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

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