1

I have three activities, A, B & C. Where A is a splash Activity and B Contains Login screen which consist of user Id and Password Text Field and one button to login. When I click on login it takes me to the welcome screen shows the user name on screen C.

Here I want to implement Shared Preference so that I can store the userid and password for the user so that user doesn't have to insert the userid and password again & again and after splash screen user directly go to welcome screen.

I read several documents about the shared preference and I came to know that there are two types of shared preference one is activity level and other one is application level.

How can I implement this?

halfer
  • 19,824
  • 17
  • 99
  • 186
vivek_Android
  • 1,697
  • 4
  • 26
  • 43

5 Answers5

3

Write it from Activity A like this:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
            Editor editor = sp.edit();
            editor.putString("YOUR_KEY", "username");
            editor.commit();

You can read it afterwards with:

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
        String username = p.getString("YOUR_KEY", null);
Macarse
  • 91,829
  • 44
  • 175
  • 230
3

This is relatively easy. You can store the username and password directly in the SharedPreference as follows:

SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);

p.edit().putString("username", username).commit();
p.edit().putString("password", password).commit(); //SECURITY HAZARD: read below...

Then you can retrieve it like this:

String username = p.getString("username", "");
String password = p.getString("password", "");

The issue when doing this is that the password is available globally. You need to have a way to prevent others from viewing it. The way you do this is by encrypting the password when you save it and decrypting it when you load it using a symmetric key. Here's a tutorial on encryption: http://android.voxisland.com/code_examples/How_to_encrypt_and_decrypt_strings.rhtml

Let me know if this helps you at all.

Emmanuel

Emmanuel
  • 16,791
  • 6
  • 48
  • 74
  • HI dear. Thanks for the reply....its one application level or activity level???and for auto remember i need to implement it in Activity A? – vivek_Android Nov 08 '10 at 14:44
  • Application level. Auto remember simply means you saved it in the preferences. When you get them you should just check if they're empty and ask the user again. – Emmanuel Nov 08 '10 at 15:06
0

This is the best way to use Shared preference just call this method

Store shared preference

public static void setDefaults(String key, String value, Context context) {
    SharedPreferences prefs =
            PreferenceManager.getDefaultSharedPreferences(context);

    SharedPreferences.Editor editor = prefs.edit();

    editor.putString(key, value);

    editor.commit();
}

Call this method and pass argument like this

Classname.setsetDefaults("key","Value",context);

Get Shared Value

    public static String getDefaults(String key, Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(key, null);
    }

Call this method And pass key

ClassName.getDefaults("Key",Context);
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67
0

to use shared preference in android

public class SharedPref {

public static void setValue(String key, String value, Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(key, value);
    editor.commit();
}

public static String getValue(String key, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(key, null);
}
 public static void setAlertDialog(Context mContext,String title,String message)
{
    AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
    alertDialog.setTitle(title);
    alertDialog.setMessage(message);
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                }
            });
    alertDialog.show();
}

}

and to set and get value from the class use following code

SharedPref.setConfig("key","value",Context);
SharedPref.getConfig("key",Context);
SharedPref.setAlertDialog(Context,"title","Content to print");
Mahesh Pandit
  • 348
  • 3
  • 7
0

Storing username and password is a bad practice instead use JWT. Take a JWT token from your response and then store it in your shared preference. If your API doesn't return any JWT in reply then at least hash your username and password before saving, but it is also unsafe.

sam
  • 1,800
  • 1
  • 25
  • 47