I\'m trying out Android Studio. Upon creating a new project and adding a default onSaveInstanceState method to the create MyActivity class, when I try to commit
In addition to the other answers, the @NonNull (and it's opponent, @Nullable) annotation annotates a field, parameter or method return type. IntelliJ and thus Android Studio can warn you for possible NullPointerExceptions at compile time.
An example is best here:
@NonNull private String myString = "Hello";
@Nullable private String myOtherString = null;
@NonNull
public Object doStuff() {
System.out.println(myString.length); // No warning
System.out.println(doSomething(myString).length); // Warning, the result might be null.
doSomething(myOtherString); // Warning, myOtherString might be null.
return myOtherString; // Warning, myOtherString might be null.
}
@Nullable
private String doSomething(@NonNull String a) {
return a.length > 1 ? null : a; // No warning
}
These annotations do not alter runtime behavior (although I have experimented with this), but serve as a tool for preventing bugs.
Note that the message you received was not an error, but just a warning, which is safe to ignore, if you choose to. The alternative is to annotate the parameter yourself as well, as Android Studio suggests:
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
}