views:

657

answers:

1

hi,

i have username,password and checkbox of remember me.

how to implement remember me function to keep username and password data??

any help would be appriciated.

+9  A: 

You can save values associated with your application using Preferences.

Define some statics to store the preference file name and the keys you're going to use:

public static final String PREFS_NAME = "MyPrefsFile";
private static final String PREF_USERNAME = "username";
private static final String PREF_PASSWORD = "password";

You'd then save the username and password as follows:

getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
        .edit()
        .putString(PREF_USERNAME, username)
        .putString(PREF_PASSWORD, password)
        .commit();

So you would retrieve them like this:

SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   
String username = pref.getString(PREF_USERNAME, null);
String password = pref.getString(PREF_PASSWORD, null);

if (username == null || password == null) {
    //Prompt for username and password
}

Alternatively, if you don't want to name a preferences file you can just use the default:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
Dave Webb
can i access those created values in other activities ???
UMMA
Yes, you can access Preferences from any of your Activities.
Dave Webb
thank you very much...
UMMA
if i create again and again same name file it will over write previous file with the same name or what??second question: can i delete this file i have created???? or set it to empty??
UMMA