Shared Preferences not retrieving value

Viewed 357

I have two activities namely Registration and Login where I defined a Shared Preferences in the Registration class. On the Registration page, if you click on the button that takes you to the Login Activity from the Registration Activity. I am storing a Shared Preferences value, so that on launching the app the second time, let the app immediately go to the LoginActivity instead of opening the first Activity which is Registration Activity. Here is my SharedPreferences class called Session.

private SharedPreferences prefs;

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
    }

    public void setusename(String usename) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("username", usename).commit();
        editor.commit();

public Boolean contKey(String key){
        if (prefs.contains(key)){
            return true;
        }else{
           return false;
        }
    }

Here is the button in the RegstrationActivity that stores the SharedPreferences value before going to the LoginActivity so that on launch the second time, it opens the LoginActivity class

loginLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                session = new Session(getApplicationContext());
                session.setusename("hello123");

                Intent intent = new Intent(getApplication(),ActivityLogin.class);
                startActivity(intent);
                finish();
            }
        });

I have defined this in the onCreate method of RegistrationActivity class to switch to the ActivityLogin screen on next time the app is launched

System.out.println("it got here...2");
        try {
            if (session.contKey("username")) {
                System.out.println("it got here...");
                Intent intent = new Intent(getApplication(), ActivityLogin.class);
                startActivity(intent);
                overridePendingTransition(R.anim.enter, R.anim.exit);
                finish();
            } else {
                System.out.println("it got here...3");
                Intent intent = new Intent(getApplication(), ActivityRegistration.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.enter, R.anim.exit);
            }
        }catch(Exception ex){

        }

Please why is my Shared Preferences not switching to the LoginActivity the second time the is launched. Kindly assist

2 Answers

TRy this

private SharedPreferences prefs;

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
    }

    public void setusename(String usename) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("username", usename);
        editor.commit();

        }

   }

    public String getusename() {
       return prefs.getString("username","");    
     }
  }

than use this way to get value SharedPreferences

session = new Session(getApplicationContext());
session.setusename("hello123");
String username=session.getusename();

Sample code

try {
            if (!session.getusename().equals("")) {
                System.out.println("it got here...");
                Intent intent = new Intent(getApplication(), ActivityLogin.class);
                startActivity(intent);
                overridePendingTransition(R.anim.enter, R.anim.exit);
                finish();
            } else {
                System.out.println("it got here...3");
                Intent intent = new Intent(getApplication(), ActivityRegistration.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.enter, R.anim.exit);
            }
        }catch(Exception ex){

        }

Instead of default shared preference, you can define a private one with your desired name in the Activity A..like as follows..

SharedPreferences preferences = getSharedPreferences("myPreferences",0);
        SharedPreferences.Editor editor= preferences.edit();
        editor.putString("userName","raju");
        editor.commit();

If you want to retrieve that value from shared preferences in Activty A(I mean in the same activity), you can use following code..

String user_name= preferences.getString("userName","N/A");

or else in Activity B(I mean in some other activty), this time you don't need editor(for retrieval)..the code will be as follows

SharedPreferences preferences = getSharedPreferences("myPreferences",0);
String user_name= preferences.getString("userName","N/A");
Related