For storing the value in the counter app you have to click once otherwise it will reset your counter to default value.
I followed this answer Continuing integer counter from sharedpreferences
I have to ask that if I close the app and again re-open the app and don't click once and then again close as it was so how can I get my counter's last session value.
Here is the code :
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String PREFS_NAME = "myPrefsFile";
TextView tv;
ImageButton btncount, btnreset;
int counter;
SharedPreferences myPrefs;
String stringCounter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tvcounter);
btncount = (ImageButton) findViewById(R.id.btncount);
btnreset = (ImageButton) findViewById(R.id.btnreset);
btncount.setOnClickListener(this);
btnreset.setOnClickListener(this);
try {
myPrefs = getSharedPreferences(PREFS_NAME, counter);
String count = myPrefs.getString("Count", "Counter");
tv.setText(count);
counter = Integer.parseInt(count);
}catch (Exception ex){
ex.printStackTrace();
}
}
@Override
public void onClick(View v) {
if (v == btncount){
counter++;
stringCounter = Integer.toString(counter);
tv.setText(stringCounter);
}
if(v == btnreset){
counter = 0;
stringCounter = Integer.toString(counter);
tv.setText(stringCounter);
}
}
@Override
protected void onPause(){
super.onPause();
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("Count", stringCounter);
editor.commit();
}
}
If I'm understanding correctly, You are getting the value from shared preferences in your OnClick method. You can init this in your onCreate method of your Activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counterTextView = findViewById(R.id.counter_tv);
if (savedInstanceState!=null) {
counterTextView.setText(String.parseInt(savedInstanceState.getInt("counter_key")))
}
This will allow you to initialize your values without having to wait for a listener to respond. If this is way off... you might try posting your own code so we can help you better.
EDIT
Thanks for the update... I see more clearly what you're trying to do. Okay, so first off, you are getting your SharedPreferences incorrectly. The INT it is asking for is the mode in which to read from the shared preferences. This developer API guide is very clear on this subject: https://developer.android.com/training/data-storage/shared-preferences.html#GetSharedPreferences
As that guide states, you don't have to create your own SharedPreference file, you can get the default one... but if you did want to do that, it should look something like this:
SharedPreferences prefs = getSharedPreferences("myPrefFile",Context.MODE_PRIVATE);
That being said, your counter is default 0 (which is == to Context.MODE_PRIVATE), so at this point, you won't see a difference once you do that.
You were very close!! You were not initializing your stringCounter variable (only on your onClick). That explains why this worked properly only when you clicked.
try {
myPrefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
String count = myPrefs.getString("Count", "Counter");
tv.setText(count);
counter = Integer.parseInt(count);
}catch (Exception ex){
ex.printStackTrace();
}
stringCounter = Integer.toString(counter);
And then in your onPause
@Override
protected void onPause(){
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("Count", stringCounter);
editor.commit();
super.onPause();
}
Anyway, hope that gets what you need! It was working correctly on my emulator after I made this change.