SharedPrefferences automatically cleaning

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 that I could catch it 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","");
      }
}
    
asked by anonymous 05.05.2016 / 04:56

1 answer

1

What I'm seeing you are not saving anything in your sharedpreferences I have an example for you I used this way in my application .. I created a "preference" class and did this

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 that

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 save the user's login and password so that it does not have to be typed all the time and when I want to erase the data of my user and password I do it this way

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 preference you have these codes to delete the information if he takes the checkbox option 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 have helped ..

    
05.05.2016 / 13:24