Skip to content

Commit

Permalink
Persisting user nickname if he starts or resumes the game with, and r…
Browse files Browse the repository at this point in the history
…estoring it on demand

Resolves: vocollapse#8
  • Loading branch information
smarek authored and cweiske committed Jan 9, 2018
1 parent 3d49574 commit e1c82f2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/org/blockinger/game/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,27 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}

private void persistNickname() {
TextView nickNameEditText = (TextView) findViewById(R.id.nicknameEditView);
if (null != nickNameEditText){
PreferenceManager.getDefaultSharedPreferences(this)
.edit().putString(PLAYERNAME_KEY, nickNameEditText.getText().toString()).commit();
}
}

private void restoreNickname() {
TextView nickNameEditText = (TextView) findViewById(R.id.nicknameEditView);
if (null != nickNameEditText){
nickNameEditText.setText(
PreferenceManager.getDefaultSharedPreferences(this)
.getString(PLAYERNAME_KEY, null)
);
}
}

public void start() {
persistNickname();
Intent intent = new Intent(this, GameActivity.class);
Bundle b = new Bundle();
b.putInt("mode", GameActivity.NEW_GAME); //Your id
Expand Down Expand Up @@ -241,6 +260,7 @@ public void onStopTrackingTouch(SeekBar arg0) {
}

public void onClickResume(View view) {
persistNickname();
Intent intent = new Intent(this, GameActivity.class);
Bundle b = new Bundle();
b.putInt("mode", GameActivity.RESUME_GAME); //Your id
Expand Down Expand Up @@ -275,6 +295,7 @@ protected void onDestroy() {
@Override
protected void onResume() {
super.onResume();
restoreNickname();
sound.setInactive(false);
sound.resume();
datasource.open();
Expand Down

0 comments on commit e1c82f2

Please sign in to comment.