KhanStan99
KhanStan99

Reputation: 424

Unable to access SharedPreferences value from a class into an activity

I am working on a server baser application so whenever a user logs in i have to capture his unique userId and use it in different activities in my whole project. So after getting the userId from JSONObject i am putting it in sharedPrederence and getting it in another activity but after entering into my main activity which is after successful login the shared preference shows me the default value of userId which is zero.

Here is my code in PostData.java class which will work in background of login activity:

private SharedPreferences userIdSharedPreferences;
private SharedPreferences.Editor userIdEditor;

              try {
                    JSONObject obj = new JSONObject(response.toString());
                    //boolean requestStatus = obj.getBoolean("requestStatus");
                    JSONObject data = obj.getJSONObject("data"); 
                    userId = Integer.parseInt(data.getString("userId"));            
                    userIdEditor = userIdSharedPreferences.edit();
                    userIdEditor.putInt("crrUserId", userId);
                    userIdEditor.apply();
                    userIdEditor.commit();

                } catch (JSONException e) {
                    e.printStackTrace();
                }

and here is how i am using that sharedPreference value in other acitivity:

userIdSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    crrUserId = userIdSharedPreferences.getInt("crrUserId", 0);
    Toast.makeText(getApplicationContext(), "Your user id is: " + crrUserId, Toast.LENGTH_SHORT).show(); //This toast showing 0 which is default i set.

EDIT

for more clarification now i am passing my userId from my background class PostData.java to my LoginActivity using the below function:

    Integer passUserId() {
    return userId;
}

and from there i am passing userId using sharedPreference like this:

Intent loginSuccess = new Intent(getApplicationContext(), MainActivity.class);
            Integer crrUserId = Login.passUserId(); //Login is name of variable which will accespassUserId function in PostData.java class
            SharedPreferences userIdSharedPreferences = getApplicationContext().getSharedPreferences("userId", 0);
            SharedPreferences.Editor userIdEditor;
            userIdEditor = userIdSharedPreferences.edit();
            userIdEditor.putInt("crrUserId", crrUserId);
            userIdEditor.apply();
            startActivity(loginSuccess);
            finish();

and here is how i am using in my mainactivity:

public class MainActivity extends AppCompatActivity {
SharedPreferences userIdSharedPreferences;
SharedPreferences.Editor userIdEditor;
Integer crrUserId;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    userIdSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    crrUserId = userIdSharedPreferences.getInt("userId", 0);
    Toast.makeText(getApplicationContext(), "Your user id is: " + crrUserId, Toast.LENGTH_SHORT).show();

}

Upvotes: 0

Views: 67

Answers (2)

nivesh shastri
nivesh shastri

Reputation: 440

Try this:

First you need to initialize userIdSharedPreferences object like below:

userIdSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

After getting object of SharedPreference do this:

userIdEditor = userIdSharedPreferences.edit();

After getting object of SharedPreferences.Editor do your task.

Upvotes: 1

Sai Jayant
Sai Jayant

Reputation: 376

initialiase userIdSharedPreferences may be thats the problem

   try {
            JSONObject obj = new JSONObject(response.toString());
            //boolean requestStatus = obj.getBoolean("requestStatus");
            JSONObject data = obj.getJSONObject("data");
            userId = Integer.parseInt(data.getString("userId"));

            SharedPreferences userIdSharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0);

            SharedPreferences.Editor userIdEditor = userIdSharedPreferences.edit();
            userIdEditor.putInt("crrUserId", userId);
            userIdEditor.apply(); //apply or commit not both
            userIdEditor.commit();

        } catch (JSONException e) {
            e.printStackTrace();
        }

Upvotes: 1

Related Questions