What is the purpose of StatementWithEmptyBody?

In case, if you have not inserted any code in that method, the annotation will make sure that no warnings are generated for that method.


Warning itself explains meaning.

return type of onNavigationItemSelected is boolean. and we need to return any boolean value.

If there is if condition in onNavigationItemSelected and not returned then @SuppressWarnings("StatementWithEmptyBody") need to add.

Example:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.xyz) {
       // you should return boolean value here.
    }
    return false;
}

In example we are returning false by-default. and we haven't return any value in

if (id == R.id.xyz) condition.

You can clearly have a look at warning.

suppress warning


When you have a statement with no code inside of the curly brackets, Android Studio will give a warning about this. The annotation suppresses this warning in method having this annotation.

Example of statement with empty body (the body of the "else" part is empty):

if (something) {
    doThis();
} else {

}

These warnings are useful to let you double check if you didn't forget to code something. Only turn them off (with the annotation) when you have a good reason to do so.