1

So I'm overriding onRestoreInstanceState() to restore data when the screen is flipped over, and inside I'm calling getString() to set the text of an instance of EditText:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    mEditText.setText(savedInstanceState.getString(TYPED_TEXT, String.valueOf(R.string.message_empty)));       

}

And then, Android Studio 1.0.2 underlines getString() and then gives me a warning: "Call requires API level 21"

The thing is, although I get a warning, when I run the app on an Android device (version 4.0.3 - API 15) it works just fine!

My min SDK is set to Froyo

My target SDK to Lollipop

Does anybody know why I'm getting that warning?

1 Answers1

0

From the BaseBundle docs

public String getString (String key, String defaultValue) Added in API level 12

Other than that the code seems fine and as you said work correctly on API 15. Perhaps you misread the "12" as "21"?

Still your code would not work below API 12. Since the restoring method is only called when the saved instance state is not null, you could use something like this instead:

mEditText.setText(savedInstanceState.getString(TYPED_TEXT));

The message_empty text would be set only if the EditText contents weren't saved in the first place... which you obviously programmed, right? Plus I assume it was an empty string.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • Hi Eugen, thank you for answering! I've changed the min SDK to 12 and the warning does not appear anymore. And yes, it showed API 21, not 12 -- I guess it's a bug. –  Feb 02 '15 at 00:13
  • No no no! If you rewrote the call as I suggested the warning would go away as well plus devices running older versions of Android could still run your app. If you raised minSDK everytime you had this kind of bug, you'd be at 21 pretty soon :D Try to work around it for the sake of compatibility. – Eugen Pechanec Feb 02 '15 at 00:38
  • Oops, you're right Eugen! I've set the min SDK back to 8 and I've changed the code as you said and the warning is gone. Thank you very much Eugen =) –  Feb 02 '15 at 00:47