I’m new here and new to android studio, hope you can help me.
I am trying to make a simple counter app.
I have a button that adds +1 to the value of my textview(int) and a button that saves that value to shared preferences, and another button that loads the value from shared preferences, and they all work fine. I ran into problem when I first open the app and add some values to textview, then click save, and reload the app. After that when I click “load” button it loads the value fine, but when I then click “Add” button it starts counting from 0 again and it overwrites the loaded value in textview, the counting starts again… What I want to do is after reloading the app, the counting to continue from loaded value from sp.
Here is my code:
public class MainActivity extends AppCompatActivity { Button btnReset, btnPlus, btnSave, btnLoad; TextView tv; int counter; String stringVal; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loadSavedPreferences(); initialiseUI(); } private void savePreferences(int score) { SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("score", score); editor.commit(); } private void loadSavedPreferences() { try { SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this); int counter = sharedPreferences.getInt("score", 0); tv.setText(Integer.toString(counter)); } catch (Exception a) { //Hevi } } private void initialiseUI() { loadSavedPreferences(); tv = (TextView) findViewById(R.id.textView1); btnLoad = (Button) findViewById(R.id.btnLoad); btnLoad.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { loadSavedPreferences(); } }); btnSave = (Button) findViewById(R.id.btnSave); btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { savePreferences(counter); } }); btnReset = (Button) findViewById(R.id.btnReset); btnReset.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { counter=0; stringVal = Integer.toString(counter); tv.setText(stringVal); savePreferences(counter); } }); btnPlus = (Button) findViewById(R.id.btnPlus); btnPlus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { counter++; stringVal = Integer.toString(counter); tv.setText(stringVal); } }); }
Also I would like better if I could load the saved value from sp on startup, without using the button, I tried that but was unsuccessful so I made the button and it worked. But I would prefer better if I can erase the button and do it without it.
Please help
Advertisement
Answer
Solution
Step 1. Change the code from
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loadSavedPreferences(); initialiseUI(); }
to
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initialiseUI(); loadSavedPreferences(); }
Step 2. Change the code from
int counter = sharedPreferences.getInt("score", 0);
to
counter = sharedPreferences.getInt("score", 0);