Chim Ly
Chim Ly

Reputation: 159

SharedPreference cannot save data across activities

I'm working on a splash screen where it will determine whether the user has previously registered or not based on SharedPreference stored values.

Please allow me to ask this question one more time, as I've gone through so many help / tutorials and examples, it didn't help a bit. I've been stuck here for 2 days now... Any help is really really appreciated.

There are 2 activities involved. The app starts with SplashScreen activity, then if sharepreference file return non-null (means user registered before), it will start mainactivity. Else if sharepreference file return null (means first time user, it brings user to the registration activity)...

PROBLEM: whenever the app restart (even with user registered), it always go to registration page !! PLEASE HELP !!

code for SPLASHSCREEN activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);

    Thread timerThread = new Thread(){
        public void run(){
            try{
                sleep(3000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{

            }
        }
    };
    timerThread.start();

}

protected void onStart() {
    super.onStart();
    openNextActivity();
}

public void openNextActivity(){

    SharedPreferences sp = getSharedPreferences("pref", 0);
    if (sp.contains("Name")) {
        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(intent);
    } else {
        Intent intent = new Intent(SplashScreen.this, Registration.class);
        startActivity(intent);
    }

}

@Override
protected void onPause() {
    super.onPause();
    finish();
}

below is the code for REGISTRATION activity...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);

    etName = (EditText) findViewById(R.id.etName);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etMobilePhone = (EditText) findViewById(R.id.etMobilePhone);
    tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // imei

    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnSubmit.setOnClickListener(this);


}


public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnSubmit:
            writePref();
            break;
    }
    finish();
}


private void writePref() {

    String userName  = etName.getText().toString();             //prepare variables values for write
    String userPhone  = etMobilePhone.getText().toString();     //prepare variables values for write
    String userEmail  = etEmail.getText().toString();           //prepare variables values for write
    String userImei = tel.getDeviceId().toString();             //prepare variables values for write

    SharedPreferences sp = getSharedPreferences("pref", 0);     //sharepreference
    SharedPreferences.Editor editor = sp.edit();                //sharepreference
    editor.putString(Name, userName);                           //write sharepreferences
    editor.putString(Phone, userPhone);                         //write sharepreferences
    editor.putString(Email, userEmail);                         //write sharepreferences
    editor.putString(Imei, userImei);                           //write sharepreferences
    editor.commit();                                            //sharepreference

    Toast toast = Toast.makeText(getApplicationContext(), "updated sharepreferences", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,0,50);
    toast.show();

}

Please guide me to the right directions. Thank you for reading and responding.

Upvotes: 1

Views: 118

Answers (2)

Viral Patel
Viral Patel

Reputation: 33438

Change your openNextActivity() method to this:

public void openNextActivity(){

    SharedPreferences sp = getSharedPreferences("pref", 0);
    String defaultValue = "na";
    String storedValue = sp.getString("Name", defaultValue);

    if (!storedValue.equalsIgnoreCase("na")) {
        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(intent);
    } else { //if you get default value i.e. "na"
        Intent intent = new Intent(SplashScreen.this, Registration.class);
        startActivity(intent);
    }

}

Instead of checking if the key exists, check for its value. If key not found return a default value and and check against the returned value.

Update (From Jelle's comment below): Also change your RegistrationActivity. Change editor.putString(Name, userName); into editor.putString("Name", userName);

Upvotes: 1

Chim Ly
Chim Ly

Reputation: 159

Thanks guys... wanted to share the solution...

SharedPreferences sp = getSharedPreferences("pref", 0);     //sharepreference
    SharedPreferences.Editor editor = sp.edit();                //sharepreference
    editor.putString("Name", userName);                           //write sharepreferences
    editor.putString("Phone", userPhone);                         //write sharepreferences
    editor.putString("Email", userEmail);                         //write sharepreferences
    editor.putString("Imei", userImei);                           //write sharepreferences
    editor.commit();                                            //sharepreference

Apparently, the solution is the ""... thanks for all the wonderful people...

Upvotes: 1

Related Questions