SharedPreferences [ Save little Data without making database ]

SharedPreferences:



for that we need help of 2 classes. 
           1. SharedPreferences
           2. SharedPreferences.Editor


1. Declaration needed Classes :

       
private SharedPreferences myPreferences;   //reference for sharedpreference class
private SharedPreferences.Editor  editor;  //by editor we can write or edit Data

2. Initialize: 

       
//Shared Preference code:
myPreferences = getSharedPreferences("user Authentication",MODE_PRIVATE); //mode private make the data private it can use only from this app.,
editor = myPreferences.edit(); //now myPreference ready for edit or write

3. Save Data:

       
public void saveData(View view) {
    String email = ET_userName.getText().toString();
    String pass =  ET_Password.getText().toString();

    editor.putString("email",email);
    editor.putString("password",pass);
    editor.commit();  // it means my job is done and i commited that.
    Toast.makeText(this, "Data Saved", Toast.LENGTH_SHORT).show();
}

4. Show Data:

       
public void showData(View view) {
    String email = myPreferences.getString("email","No such found");
    String pass = myPreferences.getString("password","No password found");

    TV_userName.setText(email);
    TV_Password.setText(pass);
}

5. Clear Data:

       
public void clearData(View view) {
    editor.clear(); //All Clear    editor.commit();

    showData(view);
}


Project Done 
👮👮👮👮👮👮👮👮👮👮👮👮👮👮👮

XML demo for above code test:


মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

API (Application Programming Interface) .

Add Gif file in Android xml file