-1

I can not use a String Value that I get from Shared Preference in if statement.

Here's a brief description:

if(jm == "Mute"){
// do something
}

This Statement works when

String jm = "Mute";

But doesn't work when

prefs = getSharedPreferences(PREF_NAME, 0);

String jm = prefs.getString("key", "");
//which returns Mute
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

use .equals in if statement to get work so change

if(jm == "Mute"){

to

if(jm.equals("Mute")){

For more info see How do I compare strings in Java?

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • @user3777565 Happy to help,Enjoy coding and please accept this answer so it may help others with similar issue. – Giru Bhai Jun 27 '14 at 17:15
0

Strings should be compared using equals(). Using == you are comparing references not the actual content of the string.

How do I compare strings in Java?

Community
  • 1
  • 1
Federico Perez
  • 976
  • 1
  • 13
  • 34