Question

I am trying to save to my apps shared preferences, I save the Username and password.

on successful login, these get stored fine. When I kill the app, then re-open it, it auto logs in as it should using the shared prefs.

however when I navigate to the account management activity and try access the prefs again the keys for the username and password can't be found and the default value is returned. I'm definetly using the right key values as it works perfectly fine when a manual login is required if there are no user/password prefs stored on first startup.

This really confuses me as the username and password keys clearly exist otherwise the it wouldn't auto-login.

Some code:

This is my account management activity:

public class AccManagement extends Activity {

TextView textName;
TextView textEmail;
String SharedUsername;
String SharedPassword;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_acc_management);
    textName = (TextView)findViewById(R.id.textviewName);
    textEmail = (TextView)findViewById(R.id.textviewEmail);

            //these return null, as if there are no shared prefs stored.
    SharedUsername= SharedPrefsWrapper.getFromPrefs(this, "USERNAME", null);
    SharedPassword= SharedPrefsWrapper.getFromPrefs(this, "PASSWORD", null);
}

I made a wrapper class for shared prefs which gets and puts key/value pairs into the shared prefs.

This is how I check if a user exists in my main activity:

public void Checker()
{

    String SharedUsername= SharedPrefsWrapper.getFromPrefs(this, "USERNAME", null);
    String SharedPassword= SharedPrefsWrapper.getFromPrefs(this, "PASSWORD", null);

    if(SharedUsername== null || SharedPassword== null)
    {
        setContentView(R.layout.activity_main);
    }
    else
    {
        Login(url);
    }
}

Login executes an http request and this method is called on completion:

   public void onTaskCompleted(String data) 
   {

    if(data.contains("name")) //a successful login will return a json string with "name".
    {
        Intent intent = new Intent(this, MenuActivity.class);
        SharedPrefsWrapper.saveToPref(this, "USERNAME", textusername);
        SharedPrefsWrapper.saveToPref(this, "PASSWORD", textpassword);
            startActivity(intent);
    }    
 }

On successful login the user is taken a menu which contains the account management button, and on click are taken to account management, which displays the username and password. Though this is where my problem occurs. It can't find the shared prefs I saved on login. It only happens when the app auto logs in using the shared prefs. If it's a first time login it seems to work fine and the shared prefs still seem to be there when I try get them from my account activity.

Thanks for any help

and sorry if the question simply doesn't make sense!

:)

EDITS:

SharedPrefs class methods:

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

public static String getFromPrefs(Context context, String key, String defValue)
{
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    try
    {
        return prefs.getString(key, defValue);
    }
    catch(Exception ex)
    {
        return defValue;
    }
}
Was it helpful?

Solution

When auto loging, textusername and textpassword are empty, so this wipes out your prefs.

Try to respect java naming conventions, your code is hard to read.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top