<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ghanim Khan</title>
    <description>The latest articles on DEV Community by Ghanim Khan (@ghanimkhan).</description>
    <link>https://dev.to/ghanimkhan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F508030%2F20b745af-b3e9-4808-8361-ceec0ccdacfe.jpeg</url>
      <title>DEV Community: Ghanim Khan</title>
      <link>https://dev.to/ghanimkhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ghanimkhan"/>
    <language>en</language>
    <item>
      <title>You were using SharedPreferences  the wrong way 🤔 </title>
      <dc:creator>Ghanim Khan</dc:creator>
      <pubDate>Fri, 06 Nov 2020 11:40:26 +0000</pubDate>
      <link>https://dev.to/ghanimkhan/you-were-using-sharedpreferences-the-wrong-way-319j</link>
      <guid>https://dev.to/ghanimkhan/you-were-using-sharedpreferences-the-wrong-way-319j</guid>
      <description>&lt;p&gt;Let's keep it easy and simple &lt;/p&gt;

&lt;h1&gt;
  
  
  Shared preferences
&lt;/h1&gt;

&lt;p&gt;Shared preferences allow you to store small amounts of primitive data as key/value pairs in a file on the device&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;keep track of small bits of important&lt;/li&gt;
&lt;li&gt;Store information without needing a storage permission&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  SharedPreferences easy explanation
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;VIDEO  &lt;a href="https://www.youtube.com/watch?v=0NTL4Z6DR8U"&gt;CLICK LINK&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IU4kpqMk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zcytujorrrgqocohxxx8.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IU4kpqMk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zcytujorrrgqocohxxx8.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  MainActivity Code
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MainActivity extends AppCompatActivity {
    TextView sharedPref;
    Button buttonAdd;
    int count=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
        count = sp.getInt("your_int_key", -1);


        sharedPref=findViewById(R.id.sharedPref);
        buttonAdd=findViewById(R.id.buttonAdd);
        sharedPref.setText(""+count);

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                sharedPref.setText(""+count);

                SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putInt("your_int_key", count);
                editor.commit();
            }
        });

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  Lets Upgrade to PreferenceUtil
&lt;/h1&gt;
&lt;h3&gt;
  
  
  you must be thinking why PreferenceUtil 🤔 , let's see why should we use this.
&lt;/h3&gt;

&lt;p&gt;1) Much cleaner and readable code.&lt;br&gt;
2) Can access any variable from any class.&lt;/p&gt;

&lt;p&gt;--&amp;gt; To get the value&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;PreferenceUtil.getInstance(MainActivity.this).getCountNum();&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;--&amp;gt; To set the value&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;PreferenceUtil.getInstance(MainActivity.this).SetCountNum(count);&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;3) Code is reduced.&lt;br&gt;
4) Variables using shared preferences are in one place.&lt;br&gt;
5) Fewer errors due to key naming.&lt;br&gt;
6) Easy to use with ViewModel.&lt;br&gt;
7) Very good for you to handle large projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  I hope these advantages are good enough for you to migrate😎.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;VIDEO  &lt;a href="https://youtu.be/7t9MM7Ctmi4"&gt;CLICK LINK&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g2CLkCx2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7cjn5fuhahv7w955cewl.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g2CLkCx2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7cjn5fuhahv7w955cewl.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  MainActivity Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.androxus.prefranceutil;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView sharedPref;
    Button buttonAdd;
    int count=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        sharedPref=findViewById(R.id.sharedPref);
        buttonAdd=findViewById(R.id.buttonAdd);


//        SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
//        count = sp.getInt("your_int_key", -1);
        count=PreferenceUtil.getInstance(MainActivity.this).getCountNum();


        sharedPref.setText(""+count);
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                sharedPref.setText(""+count);
                PreferenceUtil.getInstance(MainActivity.this).SetCountNum(count);


//                SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
//                SharedPreferences.Editor editor = sp.edit();
//                editor.putInt("your_int_key", count);
//                editor.commit();
            }
        });

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  PreferenceUtil class code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.androxus.prefranceutil;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PreferenceUtil {


    public static final String COUNT_VALUE = "count_value";
    public static final String COUNT_NUM = "count";
    private static com.androxus.prefranceutil.PreferenceUtil sInstance;
    private SharedPreferences mPref;
    private SharedPreferences.Editor mEditor;

    private PreferenceUtil(final Context context) {
        mPref = PreferenceManager.getDefaultSharedPreferences(context);
    }
    public static com.androxus.prefranceutil.PreferenceUtil getInstance(final Context context) {
        if (sInstance == null)
            sInstance = new com.androxus.prefranceutil.PreferenceUtil(context.getApplicationContext());
        return sInstance;
    }

    public final int getCountNum() {
        return mPref.getInt(COUNT_NUM,0);
    }

    public final int getCount() {
        return mPref.getInt(COUNT_VALUE,0);
    }

    public void setCount(int count) {
        final SharedPreferences.Editor editor = mPref.edit();
        editor.putInt(COUNT_VALUE, count);
        editor.apply();
    }

    public void SetCountNum(int count) {
        final SharedPreferences.Editor editor = mPref.edit();
        editor.putInt(COUNT_NUM, count);
        editor.apply();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  ENJOY HOPE YOU LIKE😁
&lt;/h1&gt;

&lt;h2&gt;
  
  
  If you have any suggestion please let me know on the details below.
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N0JJ68gP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/j2gc3apoxacjx90ubvyu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N0JJ68gP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/j2gc3apoxacjx90ubvyu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Contact us
&lt;/h3&gt;

&lt;p&gt;&lt;a href="mailto:androxus.app@gmail.com"&gt;androxus.app@gmail.com&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.instagram.com/androxus.insta/"&gt;https://www.instagram.com/androxus.insta/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Our Work
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://https://play.google.com/store/apps/dev?id=5382873575159685610"&gt;androxus&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Author Linkedin
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://https://www.linkedin.com/in/ghanim-khan-713a09131/"&gt;Linkedin&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>mvvm</category>
      <category>sharedpreferences</category>
      <category>viewmodel</category>
    </item>
  </channel>
</rss>
