I have a main app that I want to store score integer using SharedPreferences
When the app is restarted it returns the default value instead of the
previously saved value. I need some help.
The main problem here is that I have followed the steps that sharedpreferences demands, the is saved but cannot be restored on app re-open.
The default value in returned when the app is re opened ieScore=myScore.getInt ( "score",0 );
Here is the sample of the score
app code.
package tips.admin.tipsscore.score;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
TextView score;
Button add,sub;
Context context;
int Score=0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
score=(TextView)findViewById ( R.id.score );
add=(Button)findViewById ( R.id.increase );
sub=(Button)findViewById ( R.id.decrease );
SharedPreferences myScore=context.getSharedPreferences ("myscore", Context.MODE_PRIVATE);
Score=myScore.getInt ( "score",0 );
score.setText ( "SCORE : "+Score );
add.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score+=45;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );
);
sub.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score-=21;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );
);
android
add a comment |
When the app is restarted it returns the default value instead of the
previously saved value. I need some help.
The main problem here is that I have followed the steps that sharedpreferences demands, the is saved but cannot be restored on app re-open.
The default value in returned when the app is re opened ieScore=myScore.getInt ( "score",0 );
Here is the sample of the score
app code.
package tips.admin.tipsscore.score;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
TextView score;
Button add,sub;
Context context;
int Score=0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
score=(TextView)findViewById ( R.id.score );
add=(Button)findViewById ( R.id.increase );
sub=(Button)findViewById ( R.id.decrease );
SharedPreferences myScore=context.getSharedPreferences ("myscore", Context.MODE_PRIVATE);
Score=myScore.getInt ( "score",0 );
score.setText ( "SCORE : "+Score );
add.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score+=45;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );
);
sub.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score-=21;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );
);
android
1
Your key (SCORE : & score) is different while save and retrieve your score.
– Piyush
Nov 15 '18 at 11:31
the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..
– user392117
Nov 15 '18 at 11:34
Just to clarify Piyush comment: you are saving a value under"SCORE : "
key, but retrieving a value under"score"
key
– Vladyslav Matviienko
Nov 15 '18 at 11:35
SCORE : & score , i never thought of that.thank you peoples
– joseph
Nov 15 '18 at 11:44
add a comment |
When the app is restarted it returns the default value instead of the
previously saved value. I need some help.
The main problem here is that I have followed the steps that sharedpreferences demands, the is saved but cannot be restored on app re-open.
The default value in returned when the app is re opened ieScore=myScore.getInt ( "score",0 );
Here is the sample of the score
app code.
package tips.admin.tipsscore.score;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
TextView score;
Button add,sub;
Context context;
int Score=0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
score=(TextView)findViewById ( R.id.score );
add=(Button)findViewById ( R.id.increase );
sub=(Button)findViewById ( R.id.decrease );
SharedPreferences myScore=context.getSharedPreferences ("myscore", Context.MODE_PRIVATE);
Score=myScore.getInt ( "score",0 );
score.setText ( "SCORE : "+Score );
add.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score+=45;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );
);
sub.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score-=21;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );
);
android
When the app is restarted it returns the default value instead of the
previously saved value. I need some help.
The main problem here is that I have followed the steps that sharedpreferences demands, the is saved but cannot be restored on app re-open.
The default value in returned when the app is re opened ieScore=myScore.getInt ( "score",0 );
Here is the sample of the score
app code.
package tips.admin.tipsscore.score;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
TextView score;
Button add,sub;
Context context;
int Score=0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
score=(TextView)findViewById ( R.id.score );
add=(Button)findViewById ( R.id.increase );
sub=(Button)findViewById ( R.id.decrease );
SharedPreferences myScore=context.getSharedPreferences ("myscore", Context.MODE_PRIVATE);
Score=myScore.getInt ( "score",0 );
score.setText ( "SCORE : "+Score );
add.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score+=45;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );
);
sub.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score-=21;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );
);
android
android
edited Nov 15 '18 at 11:56
James Westgate
8,69654558
8,69654558
asked Nov 15 '18 at 11:28
josephjoseph
81
81
1
Your key (SCORE : & score) is different while save and retrieve your score.
– Piyush
Nov 15 '18 at 11:31
the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..
– user392117
Nov 15 '18 at 11:34
Just to clarify Piyush comment: you are saving a value under"SCORE : "
key, but retrieving a value under"score"
key
– Vladyslav Matviienko
Nov 15 '18 at 11:35
SCORE : & score , i never thought of that.thank you peoples
– joseph
Nov 15 '18 at 11:44
add a comment |
1
Your key (SCORE : & score) is different while save and retrieve your score.
– Piyush
Nov 15 '18 at 11:31
the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..
– user392117
Nov 15 '18 at 11:34
Just to clarify Piyush comment: you are saving a value under"SCORE : "
key, but retrieving a value under"score"
key
– Vladyslav Matviienko
Nov 15 '18 at 11:35
SCORE : & score , i never thought of that.thank you peoples
– joseph
Nov 15 '18 at 11:44
1
1
Your key (SCORE : & score) is different while save and retrieve your score.
– Piyush
Nov 15 '18 at 11:31
Your key (SCORE : & score) is different while save and retrieve your score.
– Piyush
Nov 15 '18 at 11:31
the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..
– user392117
Nov 15 '18 at 11:34
the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..
– user392117
Nov 15 '18 at 11:34
Just to clarify Piyush comment: you are saving a value under
"SCORE : "
key, but retrieving a value under "score"
key– Vladyslav Matviienko
Nov 15 '18 at 11:35
Just to clarify Piyush comment: you are saving a value under
"SCORE : "
key, but retrieving a value under "score"
key– Vladyslav Matviienko
Nov 15 '18 at 11:35
SCORE : & score , i never thought of that.thank you peoples
– joseph
Nov 15 '18 at 11:44
SCORE : & score , i never thought of that.thank you peoples
– joseph
Nov 15 '18 at 11:44
add a comment |
2 Answers
2
active
oldest
votes
You using a different keys for saving and restroring value from shared preferences.
You need to use a same key.
For example:
save:
editor.putInt ("score_value", Score)
;
read:
myScore.getInt ("score_value", 0);
add a comment |
Use string constants instead, to avoid misspelling your tags.
In your class declare :
private static final String NAME_OF_TAG = “name_of_tag”
Then use that string instead:
editor.putInt(NAME_OF_TAG, 3);
int value = preferences.getInt(NAME_OF_TAG, 0);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318468%2fi-have-a-main-app-that-i-want-to-store-score-integer-using-sharedpreferences%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You using a different keys for saving and restroring value from shared preferences.
You need to use a same key.
For example:
save:
editor.putInt ("score_value", Score)
;
read:
myScore.getInt ("score_value", 0);
add a comment |
You using a different keys for saving and restroring value from shared preferences.
You need to use a same key.
For example:
save:
editor.putInt ("score_value", Score)
;
read:
myScore.getInt ("score_value", 0);
add a comment |
You using a different keys for saving and restroring value from shared preferences.
You need to use a same key.
For example:
save:
editor.putInt ("score_value", Score)
;
read:
myScore.getInt ("score_value", 0);
You using a different keys for saving and restroring value from shared preferences.
You need to use a same key.
For example:
save:
editor.putInt ("score_value", Score)
;
read:
myScore.getInt ("score_value", 0);
answered Nov 15 '18 at 11:34
OnixOnix
4478
4478
add a comment |
add a comment |
Use string constants instead, to avoid misspelling your tags.
In your class declare :
private static final String NAME_OF_TAG = “name_of_tag”
Then use that string instead:
editor.putInt(NAME_OF_TAG, 3);
int value = preferences.getInt(NAME_OF_TAG, 0);
add a comment |
Use string constants instead, to avoid misspelling your tags.
In your class declare :
private static final String NAME_OF_TAG = “name_of_tag”
Then use that string instead:
editor.putInt(NAME_OF_TAG, 3);
int value = preferences.getInt(NAME_OF_TAG, 0);
add a comment |
Use string constants instead, to avoid misspelling your tags.
In your class declare :
private static final String NAME_OF_TAG = “name_of_tag”
Then use that string instead:
editor.putInt(NAME_OF_TAG, 3);
int value = preferences.getInt(NAME_OF_TAG, 0);
Use string constants instead, to avoid misspelling your tags.
In your class declare :
private static final String NAME_OF_TAG = “name_of_tag”
Then use that string instead:
editor.putInt(NAME_OF_TAG, 3);
int value = preferences.getInt(NAME_OF_TAG, 0);
answered Nov 15 '18 at 11:40
Nikos HidalgoNikos Hidalgo
1,0901317
1,0901317
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318468%2fi-have-a-main-app-that-i-want-to-store-score-integer-using-sharedpreferences%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Your key (SCORE : & score) is different while save and retrieve your score.
– Piyush
Nov 15 '18 at 11:31
the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..
– user392117
Nov 15 '18 at 11:34
Just to clarify Piyush comment: you are saving a value under
"SCORE : "
key, but retrieving a value under"score"
key– Vladyslav Matviienko
Nov 15 '18 at 11:35
SCORE : & score , i never thought of that.thank you peoples
– joseph
Nov 15 '18 at 11:44