I need to commit()
rather than apply()
my changes to my SharedPreferences.Editor
:
SharedPreferences sharedPrefs = getSharedPreferences("MY_SHARED_PREFS_FILE_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor sharedPrefsEditor = sharedPrefs.edit();
sharedPrefsEditor.putBoolean("MY_BOOLEAN", true);
sharedPrefsEditor.commit(); // <-- I get the Lint warning on this line.
But Lint gives me this warning:
Consider using apply instead; commit writes its data to persistent storage immediately, whereas apply will handle it in the background...
How do I suppress that Lint warning?
Adding @SuppressWarnings("all")
before my method signature suppresses the warning, but is there a more specific String I can use instead of "all"
? (Could not find anything here.)
@SuppressLint("ApplySharedPref")
works for me.
Any time you need to suppress a lint, what you have to do is to find the Lint issue id and pass it to this annotation.
You can find it by going to File > Settings > Editor > Code Style > Inspections > Android > Lint. There you can search for the lint you want to suppress and, after selection it, you will be able to see its issue id
on the Description
area.
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