TemporaryName
TemporaryName

Reputation: 505

Multiple themes in fragment

Can anybody tell me why this isn't working? I want it to load a specific layout depending on what page is loaded. The if clause seems to be faulty. It's always going to the else.

     wordArray = getResources().getStringArray(resId);
     int resIdBack = getResources().getIdentifier(wordArray[5], "drawable", "com.bobgle.libretto");

    // If Background Image is dark use light theme, if not use dark theme.
    if(wordArray[5] == "libretto"){
        view = inflater.inflate(R.layout.activity_fragment_dark, container, false);
    } else {
        view = inflater.inflate(R.layout.activity_fragment_light, container, false);
    }

Upvotes: 0

Views: 49

Answers (1)

Rawa
Rawa

Reputation: 13906

When comparing strings in Java you should use the equals method.

When using equal is good to remember that it can cause nullpointer if the object you are using equals on are null (duh!) so if you got a fixed string, that can't be null always use that first.

"libretto".equals(wordArray[5]) //Can't cause nullpointerexception.
wordArray[5].equals("libretto") //Can cause nullpointerexception if wordArray[5] is null.

This SO question explains why that is the case.

Upvotes: 1

Related Questions