Search code examples
androidsuppress-warnings

What is the purpose of StatementWithEmptyBody?


when I created the drawer bar in Android Studio for my project, in the code for the select items put the next:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

But what is the use of this annotation?


Solution

  • 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