Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving problems saving boolean and int to sharedPrefs
    text
    copied!<p>I'm trying to create an endgame activity for my Hangman game for android and i'm having some trouble committing values OTHER than strings.</p> <p>Here is my main activity:</p> <pre><code>package com.assignment.hangman; import android.app.Activity; public class HangmanActivity extends Activity { public static final String GAME_PREFERENCES = "Game Preferences"; public static final String GAME_LOGIC = "Game Logic"; public static final String GAME_LOGIC_GUESS = "Guessed letter"; public static final String GAME_LOGIC_SCORE_STRING = "Unknow score"; public static final boolean GAME_LOGIC_WIN_LOOSE = false; } </code></pre> <p>I get the sharedprefs like this:</p> <pre><code>mGameSettings = getSharedPreferences("GAME_PREFERENCES", Context.MODE_PRIVATE); </code></pre> <p>And this is where something goes wrong when committing the changes to the editor:</p> <pre><code>public void finishGame() { //Commit different game variables so they can be used in the end game activity Editor editor = mGameSettings.edit(); editor.putString(GAME_LOGIC_SCORE_STRING, (tries + " of " + numberOfLives + " used")); if (tries != numberOfLives){ editor.putBoolean("GAME_LOGIC_WIN_LOOSE", true); } editor.commit(); // Launch end game Activity startActivity(new Intent(HangmanGameActivity.this, HangmanEndActivity.class)); } </code></pre> <p>And after changing activity i refetch the values like this:</p> <pre><code> if (mGameSettings.contains("GAME_LOGIC_WIN_LOOSE")) { Log.i(GAME_DEBUG, "Succes"); boolean winLoose = mGameSettings.getBoolean("GAME_LOGIC_WIN_LOOSE", false); if (winLoose) { winLooseView.setText(R.string.you_win); } else { winLooseView.setText(R.string.you_loose); } } </code></pre> <p>But somehow only the String is being committed correctly. I guess the boolean value reverts to the default value of false. </p> <p>Could someone help me shed some light on this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload