× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to handle SharedPreferences nicely in Android

Ishan Khanna
Feb 03, 2016
<pre><span style="color:#cc7832">import </span>android.content.Context<span style="color:#cc7832">; </span><span style="color:#cc7832">import </span>android.content.SharedPreferences<span style="color:#cc7832">; </span><span style="color:#cc7832">import </span>android.preference.PreferenceManager<span style="color:#cc7832">; </span> <em>/** </em><em> * Created by ishan on 1/21/16. </em><em> */ </em><span style="color:#cc7832">public class </span>SCPreferenceHelper { <span style="color:#cc7832">private static </span>SCPreferenceHelper <em>instance</em><span style="color:#cc7832">; </span><span style="color:#cc7832"> private </span>SharedPreferences <span style="color:#9876aa">sharedPreferences</span><span style="color:#cc7832">; </span> <span style="color:#cc7832"> private </span>SCPreferenceHelper (Context context) { <span style="color:#9876aa">sharedPreferences </span>= PreferenceManager.<em>getDefaultSharedPreferences</em>(context)<span style="color:#cc7832">; </span><span style="color:#cc7832"> </span>} <span style="color:#cc7832">public static </span>SCPreferenceHelper <span style="color:#ffc66d">getInstance</span>(Context context){ <span style="color:#cc7832">if </span>(<em>instance </em>== <span style="color:#cc7832">null</span>) { <em>instance </em>= <span style="color:#cc7832">new </span>SCPreferenceHelper(context)<span style="color:#cc7832">; </span><span style="color:#cc7832"> </span>} <span style="color:#cc7832">return </span><em>instance</em><span style="color:#cc7832">; </span><span style="color:#cc7832"> </span>} <span style="color:#cc7832">public boolean </span><span style="color:#ffc66d">isAppStartingFirstTime</span>() { <span style="color:#cc7832">return </span><span style="color:#9876aa">sharedPreferences</span>.getBoolean(PreferenceConstants.<em>IS_FIRST_TIME</em><span style="color:#cc7832">, true</span>)<span style="color:#cc7832">; </span><span style="color:#cc7832"> </span>} <span style="color:#cc7832">public void </span><span style="color:#ffc66d">setFirstTimePreferenceAsFalse</span>() { writeBoolean(PreferenceConstants.<em>IS_FIRST_TIME</em><span style="color:#cc7832">, false</span>)<span style="color:#cc7832">; </span><span style="color:#cc7832"> </span>} <span style="color:#cc7832">private void </span><span style="color:#ffc66d">writeBoolean</span>(String key<span style="color:#cc7832">, boolean </span>value) { <span style="color:#9876aa">sharedPreferences</span>.edit().putBoolean(key<span style="color:#cc7832">, </span>value).apply()<span style="color:#cc7832">; </span> <span style="color:#cc7832"> </span>} } </pre> <p> </p> <p>Usage </p> <pre>SCPreferenceHelper scPreferenceHelper = SCPreferenceHelper.<em>getInstance</em>(<span style="color:#cc7832">this</span>)<span style="color:#cc7832">; </span> <span style="color:#cc7832">if </span>(scPreferenceHelper.isAppStartingFirstTime()) { // Do Something<span style="color:#cc7832"> </span>}</pre>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus