1

I try the sharedPreferences option and works when you don't switch off the tablet or the phone.. in the moment that you turn off the phone all the variables are deleted.

I must save those variables always. But momently, in my present solution, I can't put it to initialized, the user's could have different variable. I must initialized with the last session variables.

How could I do that?

Zior
  • 21
  • 2
  • what? when i save in SharedPreferences, they stay there until the app is uninstalled or I do delete them values by myself... You´re doing something wrong... – eduyayo May 19 '15 at 11:26
  • 1
    please have a look at http://stackoverflow.com/questions/11776612/android-sharedpreferences-after-reboot-device ... it might help – Mithun May 19 '15 at 11:27
  • Have You checked my ans ..is it usefull??? – Tufan May 19 '15 at 11:59
  • Yes! thanks million! all is working perfectly. Now I have another questions. Do you know if it is able to save two kinds of sharePreference variable? What I mean, imagine that I want to save the user details and on the other hand what the user add to the app. But i cant do it in the same variable. – Zior May 20 '15 at 07:50

1 Answers1

0

you can use SharedPreferences As A session variable

To creating a session you can use

 SharedPreferences pref = getApplicationContext().getSharedPreferences("Session Data", MODE_PRIVATE);
 Editor editor = pref.edit();
 editor.putString(KEY_USERNAME,username);
 editor.putString(KEY_PASSWORD,password);
 editor.commit();

To Remove From Session You Can Use

SharedPreferences sp=getSharedPreferences("Session Data",0);            
SharedPreferences.Editor  ed=sp.edit();
ed.remove("username");
ed.remove("password");
ed.commit();

And If You Want to fetch values from shared preferences than use something like this

SharedPreferences sp;
myContext=this;
sp=myContext.getSharedPreferences("Session Data",myContext.MODE_PRIVATE);
username=sp.getString("username","");
password=sp.getString("password","");
Tufan
  • 3,384
  • 4
  • 32
  • 53