0

In my app I get a bug that makes me unable to load the SharedPreferences. The reason this happens is that when the applications is killed for good(task killer or phone restart) the phone can not load everything again. For now I am using this technique:

if ((sharedPreferences.getString("EXA1", "")) == "Example1"){

//do something
 }
else if ((sharedPreferences.getString("EXA1", "")) == "Example2"){

 //do something
 }
 else if ((sharedPreferences.getString("EXA1", "")) == "Example3"){

 //do something
}
else{
//do nothing
}

Since I got around 75 else if statements my phone refuses to load them after the app is killed. Are there any more efficient way of loading and then do something?(Note: I got more then one single SharedPreference)

Magakahn
  • 498
  • 9
  • 31
  • You say that you have more than one pref, but I would still read them into variables before doing youre else-ifs... In the sample you provided above, you load the same pref 3 times. – Alxandr Jul 21 '12 at 15:34
  • I got five different "EXA1" like "EXA2","EXA3" and so on. Can you show me an example of variables being used. – Magakahn Jul 21 '12 at 15:37
  • possible duplicate of [SharedPreferences does not work if many is used?](http://stackoverflow.com/questions/11544275/sharedpreferences-does-not-work-if-many-is-used) – taxeeta Jul 24 '13 at 03:36

1 Answers1

2

Use strObject.equals("MatchString") method

See:

if ((sharedPreferences.getString("EXA1", "")).equals("Example1")){
                                            ^^^^^^^^^^^^^^^^^^^^

//do something
 }
else if ((sharedPreferences.getString("EXA1", "")).equals("Example2")){

 //do something
 }
 else if ((sharedPreferences.getString("EXA1", "")).equals("Example3")){

 //do something
}
else{
//do nothing
}

You can not compare two String object using == operator, because it is NOT Primitive Data Type.

Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33