Sharedprefferences cleaning automatically

Asked

Viewed 87 times

0

I have a class that manages sharedpreferrences, I realized that when I close the application and open again, the sharedprefferences settings are restored to the default. When in fact the value should be saved, so I could pick it up at other times, correct?

public class SessionMananger {
     public SessionMananger(Context context){
                this._context = context;
                pref = _context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
                editor = pref.edit();
        }

       public void setClientID(String id){
         editor.putString("id",id);
         editor.commit();
        }

      public String getClientID(){
        return pref.getString("id","");
      }
}
  • It would be nice to show how you’re making these recordings on Editor, but I believe that Tiago’s answer should already solve the problem.

1 answer

1

by what I see you are not saving anything in your sharedpreferences I have an example for you I used this way in my application.. I created a class of "preference"

public class Preferencia {
    public static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences("energy", Context.MODE_PRIVATE);
    } 

    public static String getlogin(Context context) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        return sharedPref.getString("Login", "");
    }

    //// preferencia de senha
    public static void setsenha(Context context, String senha) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("Senha", senha);
        editor.commit();
    }

    public static String getsenha(Context context) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        return sharedPref.getString("Senha", "");
    }

    public static boolean setcbremember(Context context, boolean a) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("Cbremember", a);
        editor.commit();
        return a;    
    }

    public static boolean getcbremember(Context context) {
        SharedPreferences sharedPref = getSharedPreferences(context);
        return sharedPref.getBoolean("Cbremember", true);
    }    
}

and where I want you to save something I do so

public void btnentrarclick(View currentButton) 
{
    if(cbremember.isChecked()) {
        AcessoLogin ac = new AcessoLogin(Tela_de_Login.this,
                edtlogin.getText().toString(), edtsenha.getText().toString());
        Preferencia.setlogin(Tela_de_Login.this, edtlogin.getText().toString());
        Preferencia.setsenha(Tela_de_Login.this, edtsenha.getText().toString());
        Preferencia.setcbremember(Tela_de_Login.this, cbremember.isChecked());

        ac.execute("");
    }

In my case I saved login and password of the user so that it does not need to be typing all the time and when I want to erase my user data and password I do so

else{
    AcessoLogin ac = new AcessoLogin(Tela_de_Login.this,
                edtlogin.getText().toString(), edtsenha.getText().toString());
        Preferencia.deletar(Tela_de_Login.this, edtlogin.getText().toString());
        Preferencia.deletar(Tela_de_Login.this, edtsenha.getText().toString());

        ac.execute("");
}

and in the preference have these codes to delete the information if it takes the option of the checkbox in my example of course...

//// Deletar preferencia de login e senha
public static void deletar(Context context, String login) {
    SharedPreferences sharedPref = getSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.clear();
    editor.commit();
}

I hope I’ve helped..

  • I’m saving yes, sorry I didn’t show you the code but I do it anyway, giving the commit();

  • But it helped the code I posted for you?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.