Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to properly suppress Lint warning - "Consider using apply()" - when calling commit() on SharedPreferences.Editor?

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.)

like image 963
ban-geoengineering Avatar asked Feb 09 '18 21:02

ban-geoengineering


1 Answers

@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.

like image 89
CommonsWare Avatar answered Oct 04 '22 01:10

CommonsWare