Shared Preferences In Android Studio

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

To get a SharedPreferences object for your application, use one of two methods: getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter. getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.

To write values:

1. Call edit() to get a SharedPreferences.Editor
2. Add values with methods such as putBoolean() and putString().
3. Commit the new values with commit()
To read values, use SharedPref getBoolean() and getString().

The Example of SharedPreferences is given below

Java Snippet

public class MainActivity extends Activity{
LinearLayout ll;
SharedPreferences sp;
    Resources r ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
        r = getResources();
ll = (LinearLayout)findViewById(R.id.ll1);
sp = getSharedPreferences("my_bg_data",MODE_PRIVATE);
int value = sp.getInt("k1",123);
        switch (value)
        {
            case 1:
            {
ll.setBackground(r.getDrawable(R.drawable.sonic));
                break;
            }
            case 2:
            {
ll.setBackground(r.getDrawable(R.drawable.mario));
                break;
            }
            case 3:
            {
ll.setBackground(r.getDrawable(R.drawable.rat));
            }
        }
    }
    public void change(View v)
    {
SharedPreferences.Editor ed = sp.edit();
int id = v.getId();
        switch (id)
        {
            case R.id.rb1:
            {
Drawable d = r.getDrawable(R.drawable.sonic);
ll.setBackground(d);
ed.putInt("k1",1);
                break;
            }
            case R.id.rb2:
            {
Drawable d = r.getDrawable(R.drawable.mario);
ll.setBackground(d);
ed.putInt("k1",2);
                break;
            }
            case R.id.rb3:
            {
ll.setBackground(r.getDrawable(R.drawable.rat));
ed.putInt("k1",3);
            }
        }
ed.commit();
    }
}


MainActivity
public class MainActivity extends Activity
{
EditText et1,et2;
SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
        et1 = (EditText)findViewById(R.id.username);
        et2 = (EditText)findViewById(R.id.pass);
sp = getSharedPreferences("login",MODE_PRIVATE);
boolean res = sp.getBoolean("k1",false);
        if (res)
        {
            Intent i = new Intent(this,WelcomeActivity.class);
startActivity(i);
        }
    }
    public void loginCheck(View v)
    {
SharedPreferences.Editor ed = sp.edit();
        String username = et1.getText().toString().trim();
        String password = et2.getText().toString().trim();
        if (username.equals("Sathya"))
        {
            if (password.equals("tech"))
            {
                Intent i = new Intent(this,WelcomeActivity.class);
startActivity(i);
ed.putBoolean("k1", true);
ed.commit();
                finish();
            }
            else
            {
                et2.setError("Invalid Password");
            }
        }
        else
        {
            et1.setError("Invlaid Username");
        }
    }
}
WelcomeActivity
public class WelcomeActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_layout);
    }
    public void logoutUser(View v)
    {
SharedPreferences sp = getSharedPreferences("login",MODE_PRIVATE);
SharedPreferences.Editor ed = sp.edit();
ed.putBoolean("k1", false);
ed.commit();
        Intent i = new Intent(this,MainActivity.class);
startActivity(i);
        finish();
    }
}
I hope it helps!

If you need any specific tutorial in Android (Java or Kotlin), or PHP, comment below! 
Comment via Facebook
0 Comment via Google

0 تعليقات:

Post a Comment