I saw this video on how to create fragments in Android and I'm unable to understand the meaning of @Nullable here. Also in the video (thenewboston) @Nullable was used on parameter:
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState)
{
return super.onCreateView(inflater, container, savedInstanceState);
}
But later on when I tried to create the same override method Android Studio generated this:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
}
Can someone please explain me the use of @Nullable here?
@NullableMethod calls that can return null. Variables (fields, local variables, and parameters), that can be null.
Denotes that a parameter, field or method return value can be null. When decorating a method call parameter, this denotes that the parameter can legitimately be null and the method will gracefully deal with it. Typically used on optional parameters.
Annotation Type NullableA common Spring annotation to declare that annotated elements can be null under some circumstance. Leverages JSR-305 meta-annotations to indicate nullability in Java to common tools with JSR-305 support and used by Kotlin to infer nullability of Spring API.
@NonNull – The compiler can determine cases where a code path might receive a null value, without ever having to debug a NullPointerException. @ReadOnly – The compiler will flag any attempt to change the object. This is similar to Collections.
It means that the return value of onCreateView
method can be null
.
From the Android documentation:
Denotes that a parameter, field or method return value can be null.
When decorating a method call parameter, this denotes that the parameter can legitimately be null and the method will gracefully deal with it. Typically used on optional parameters.
When decorating a method, this denotes the method might legitimately return null.
This is a marker annotation and it has no specific attributes.
You can find more details and examples in this Android Support Annotation page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With