Search code examples
javaandroidandroid-studioandroid-activity

What does `@Nullable @org.jetbrains.annotations.Nullable Intent data` do and is it necessary?


I got an auto-generated override like this:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

The @Nullable @org.jetbrains.annotations.Nullable Intent data part looks curious and I'm not sure if it's necessary for it to exist. Does Android Studio need @org.jetbrains.annotations.Nullable to properly function?


Solution

  • No, it's not necessary to add @Nullable annotation in this method. This is just to let the programmer know that data could be null in some point. This is helpful also for the compiler, if you add the @Nullable and you don't check if it's null the compiler might warn you that you could get a NullPointerException.

    Edit

    Perhaps this Annotating Type Arguments And Type Parameters helps you to understand the difference. (this is only for kotlin)

    Also there's the two types :

    1.- Jetbrains Annotations

    2.- Android Annotations

    But roughly is because if you use the jetbrains it will warn you on intelIJ IDEs, for instance if you run a lint code via terminal it won't warn you, and the android one it will warn you even if you run it on pipelines, lint, etc.

    So perhaps is better to use androidx.annotation.Nullable instead, that is more precise.