Maurício Giordano
Maurício Giordano

Reputation: 3276

SharedPreferences doesn't seem to work

Here is my Contacts activity (my main activity):

public class Contacts extends Delegate {

    private static final String TAG = "Contacts";
    public View listContactsView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        String restoredText = prefs.getString("token", null);

        if(restoredText == null)
        {
            Intent intent = new Intent(this, SignIn.class);
            startActivity(intent);
        }

        setContentView(R.layout.activity_contacts);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new ContactListFragment()).commit();
        }

        //new InEventAPI(this).execute("");
    }
    ...
    ...

Here is my SignIn activity:

public SignIn extends Delegate {
    ...
    ...


    public void personSignInDelegate(HttpResponse response, JSONObject result)
    {
        if(response != null && result != null)
        {
            switch(response.getStatusLine().getStatusCode())
            {
            case 200:
                try {

                    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
                    editor.putString("token", result.get("tokenID").toString());
                    editor.commit();

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }               
                break;
            case 401:
                Toast.makeText(this, R.string.toastEmailPasswordIncorrect, Toast.LENGTH_LONG).show();
                break;
            }
        }
        else
        {
            Log.d(TAG, "Something went wrong!");
        }
    }

When I sign in, it commits on SharedPreferences, but when I close my app and re-open, the String becomes null and my OnCreate intents to SignIn again.

Is something that I'm missing?

Just to avoid doubts, my Delegate class:

public class Delegate extends ActionBarActivity {

    protected InEventAPI api;

    public Delegate() {}

    public void personSignInDelegate(HttpResponse response, JSONObject result) {};

}

Upvotes: 1

Views: 68

Answers (1)

A--C
A--C

Reputation: 36449

The problem is most likely with your use of getPreferences(). From the documentation:

Retrieve a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.

Although both classes extend the Delegate class, they are both unique classes with unique names. This means that getPreferences() in Contacts returns a different SharedPreferenceObject compared to SignIn.

Either use getSharedPreferences(String, int) Eg. instead of

getPreferences(MODE_PRIVATE);

change it to

getSharedPreferences ("OneSharedPreference", MODE_PRIVATE);

or override getPreferences() in Delegate so it calls getSharedPreferences() with a unique name.

Alternatively, if you're not using the default SharedPreferences for anything (this is usually used by any PreferenceActivity classes), you can always call

PreferenceManager.getDefaultSharedPreferences() and pass in a Context instance.

Upvotes: 2

Related Questions