Saving data in application - java

I have made an application. It's a button that shows the time you have pressed it. Every time I "kill" the application, the timer starts at 0 again (naturally). How can I make the application save the time the button is pressed, so when the application is killed, and then you open it, the timer is at that time you stopped.I have red some about how this is done, and I think it has something to do with SharedPreferences.
My code:
public class MainActivity extends ActionBarActivity {
Button button1;
Chronometer chromo;
protected long time;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
chromo=(Chronometer)findViewById(R.id.chromo);
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
chromo.setBase(SystemClock.elapsedRealtime()+time);
chromo.start();
}
else if( event.getAction() == MotionEvent.ACTION_UP){
time =chromo.getBase()-SystemClock.elapsedRealtime();
chromo.stop();
}
return true;
}
});
}}

Saving in SharedPreferences :
SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE);
// We use an editor to insert values in SharedPreferences
Editor editor = prefs.edit();
// Saving the values
editor.putLong("myTime", time);
// Committing the changes
editor.commit();
Retrieving saved values :
long savedValue = 0l;
SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE);
if (prefs.contains("hello")){
savedValue = sharedpreferences.getLong("myTime", 0l));
}

EDIT :
public class MainActivity extends ActionBarActivity {
Button button1;
Chronometer chromo;
protected long time = 0;
private SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
chromo=(Chronometer)findViewById(R.id.chromo);
prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
long savedValue = prefs.getLong("my_chrono", 0);
if(savedValue == 0)
chromo.setBase(SystemClock.elapsedRealtime());
else
chromo.setBase(SystemClock.elapsedRealtime() + savedValue);
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
chromo.start();
}
else if( event.getAction() == MotionEvent.ACTION_UP){
time =chromo.getBase()-SystemClock.elapsedRealtime();
chromo.stop();
prefs.edit().putLong("my_chrono", time).apply();
}
return true;
}
});
}}
============================================================================
To use the shared preferences, initialize this in you onCreate
SharedPreferences prefs = getSharedPreferences("the_package_of_your_app", Context.MODE_PRIVATE);
Then, try to get the saved value
int my_saved_value = prefs.getInt("the_package_of_your_app.my_int_1", 0);
if(my_saved_value != 0)
//your value of your timer was saved, do what's needed with it
else
//there was no value saved, or the timer was at 0
Now you have to save that value when needed (when the timer is stopped, or the application is closed)
prefs.edit().putInt("the_package_of_your_app.my_int_1", my_value).apply();

To elaborate on #2Dee's answer:
SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE);
// We use an editor to insert values in SharedPreferences
Editor editor = prefs.edit();
// Saving the values
editor.putLong("myTime", time);
// Committing the changes
editor.commit();
can go into the
protected void onDestroy();
method. This method can be overloaded in an Activity to be called as the activity is destroyed (killed, closed, etc) so that any data may be saved (which is what you want to do).
Likewise,
SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE);
time = sharedpreferences.getLong("myTime", 0l);
can go into the
protected void onCreate(Bundle savedInstanceState);
method. This method is called when the activity is first created. This will set your time to the saved value (defaulting to 0 if there is none).
If for some reason these need to be called at different times (such as later or earlier in the Activity's lifecycle) you can read more about it here.
If you like this answer, please upvote 2Dee's answer as well. Some of the code is literally copy/pasted from there.
Happy Coding! Leave a comment if you have more questions.

Related

Problem with saving checkedtextview state, it misbehaves with multiple items

I made an activity consisting of CheckTextView's and TextView's. When the user checks the box, I want to save that state when the user leaves the activity or closes the app.
I added onClickListener to every CTV.
Then I try to save it in onPause and onResume methods. I can't troubleshoot this problem as the checkboxes work when I save just a few of them it works (it varies but it works with 1-5 of them) but when I add all of them they are not saved when I go back to the activity.
//this will always work and will save the state of the boxes
protected void onPause() {
super.onPause();
save(ctv1.isChecked());
save(ctv2.isChecked());
save(ctv3.isChecked());
}
protected void onResume() {
super.onResume();
ctv1.setChecked(load());
ctv2.setChecked(load());
ctv3.setChecked(load());
}
//when I add all of them, they are always either checked or unchecked
//it doesn't matter what combination of them I try, it seems that it is //always working with a couple of CTV's but fails with more than 5-6 of them
//this is how my onClickListener looks like
CheckedTextView ctv1 = (CheckedTextView) findViewById(R.id.ctvFOX1);
ctv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ctv1.isChecked()) {
ctv1.setChecked(false);
}
else {
ctv1.setChecked(true);
}
}
});
//save and load methods
private void save(final boolean isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check", isChecked);
editor.apply();
}
private boolean load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean("check", false);
}
Because you only use one key to save the CheckedTextView's value!
private void save(final boolean isChecked, String key) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.apply();
}
private boolean load(String key) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(key, false);
}
protected void onPause() {
super.onPause();
save(ctv1.isChecked(), "check1");
save(ctv2.isChecked(), "check2");
save(ctv3.isChecked(), "check3");
}
protected void onResume() {
super.onResume();
ctv1.setChecked(load("check1"));
ctv2.setChecked(load("check2"));
ctv3.setChecked(load("check3"));
}

Problem to remove a backup - ANDROID STUDIO

Intent intent1 = new Intent(Questions.this, Questions.class);
startActivity(intent1);
Little problem in my learning.
Sorry for my frenchglish ^^
A variable changes every time, i press a button, it backs up and assigns it ++.
In the button input if the variable == in table REPONSE.Length, It restarts the activity and it REMOVE the backup.
My problem is that the backup does not remove itself while the activity restarts well.
Every time i support the activity it raises again without being able to start again at stage 0.
int REPONSE[]= new int[5]; //tableau des reponses
int Question = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
Question = sharedPreferences.getInt("num", 0);
cardView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Restart si Question == REPONSE.length
if (Question == REPONSE.length){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("num");
editor.apply();
Intent intent1 = new Intent(Questions.this, Questions.class);
startActivity(intent1);
}
//Sauvegarde de la variable
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("num", Question++);
editor.apply();
//Incrementation +1
Question++;
}
}); }
Thanks in advance:)
According to the documentation remove() removes a value once commit() is called. So you have to change editor.apply() to editor.commit()
//Restart si Question == REPONSE.length
if (Question == REPONSE.length){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("num");
editor.commit(); //editor.apply() won't work
There are 3 points that I wold recommend you:
Point 1:
Try giving your Shared preference a name. Example:
sharedPreferences = getSharedPreferences("sharedPrefName", MODE_PRIVATE);
If you are not giving a name to the shared preference android can fall into ambiguity and create a new Sharedpreference thus not affecting the old one.
Even you are creating a new SharedPreference inside the onClick method(this process is wrong), and there the android system is not being able to understand which Shared Preference to use thus not affecting the sharedpreference data that you want to change.
Point 2:
This not so important as the first one but to change the data of an already existing preference you need not to delete the preference instead just change the value, and it will be updated to your requirement:
sharedPreferences.edit().putInt("num", Question++).apply();
Point 3:
Create SharedPreference object once inside the class where it can
have global scope.
Initialize the SharedPreference only once in an activity inside
onCreate method.
Make your code something like this:
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
ConstraintLayout layout;
int Question = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
layout = findViewById(R.id.layout);
sharedPreferences = getSharedPreferences("sharedPrefName", MODE_PRIVATE);
Question = sharedPreferences.getInt("num",0);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sharedPreferences.edit().putInt("num", Question++).apply();
}
});
}
}

SharedPreferences integer addition

I have a code... Its excellent save data and load data, but... When i reset application, my score loading, but when i click button for +5 score, my score reset and set 5. I am want that addition +5, but its dont work...
I understand that the problem of addition, because save and load working excellent, but addition doesnt work.
Sorry for my bad English :)
int mCounts;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appli);
Settings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
int mCounts = Settings.getInt(APP_PREFERENCES_SCORE, 1);
score = (TextView) findViewById(R.id.score);
score.setText(String.valueOf(mCounts));
}
public void five(View view) {
score.setText(String.valueOf(mCounts += 5)+"");
}
public void onPause() {
super.onPause();
SharedPreferences.Editor editor = Settings.edit();
editor.putInt(APP_PREFERENCES_SCORE, mCounts);
editor.apply();
}
Try this code in your button:
public void five(View view) {
score.setText(String.valueOf(mCounts += 5)+"");
SharedPreferences.Editor editor = Settings.edit();
editor.putInt(APP_PREFERENCES_SCORE, mCounts);
editor.apply();
}
Problem is here:
int mCounts = Settings.getInt(APP_PREFERENCES_SCORE, 1);
Remove int. it will work

Counter restarts after killing app

I have made an app that starts a counter when I hold the button, and stops counting as soon as I release the button, then start again as soon as I touch it again.. (The app is made to see how long time I can use, to touch a button.)
Anyway, I have made a way to save the data of the counter, so when I kill the app or press the "back button", the data of the counter saves. BUT as soon as I tap the button again it restarts! I can't find a way to fix this. I think it has to do something with:
chromo.setBase(SystemClock.elapsedRealtime()+time);
(Found under "ACTION_DOWN") I've used chronometer as my counter by the way. Please help me!
Here's my code:
public class MainActivity extends ActionBarActivity {
Button button1;
Chronometer chromo;
protected long time = 0;
private SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
chromo=(Chronometer)findViewById(R.id.chromo);
prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
long savedValue = prefs.getLong("my_chrono", 0);
if(savedValue == 0)
chromo.setBase(SystemClock.elapsedRealtime());
else
chromo.setBase(SystemClock.elapsedRealtime() + savedValue);
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
chromo.setBase(SystemClock.elapsedRealtime()+time);
chromo.start();
}
else if( event.getAction() == MotionEvent.ACTION_UP){
time =chromo.getBase()-SystemClock.elapsedRealtime();
chromo.stop();
prefs.edit().putLong("my_chrono", time).apply();
}
return true;
}
});
}}

Does this snippet cause an infinite loop or other?

Only on old android devices (2.x) I've a crash caused by stackoverflow everytime I rotate emulator. If I comment "preferenze()" emulator does not crash but app does not keep new settings. Can this code create an infinite loop? Is a incorrect code? What should be to runs correctly? Thanks!
private boolean preferencesChanged;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
private void preferenze() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
CheckboxPreference = prefs.getBoolean("checkboxPref", true);
ListPreference = prefs.getString("listpref", "");
numeronotifiche = prefs.getString("notify", "");
Sound = prefs.getString("sound", "");
barranotifiche = prefs.getBoolean("keep", false);
natura = prefs.getBoolean("suoninaturasino", false);
snatura = prefs.getString("suoninaturascelta", "");
snaturaold = prefs.getString("snaturaoldvalue", "");
if (snaturaold != snatura){
stopService(new Intent(this, UnUsedService.class));
}
SharedPreferences prefs2 = getSharedPreferences(PRIVATE_PREF, 0);
Editor editor10 = prefs2.edit();
editor10.putString("snaturaoldvalue", snatura);
editor10.commit();
// suoni attivati (o no)
if (natura){
startService(new Intent(this, UnUsedService.class));
}
else {
stopService(new Intent(this, UnUsedService.class));
}
if (barranotifiche){
showNotification();
}
else {
cancelNotification();
}
GestioneAllarme alarm = new GestioneAllarme();
if (CheckboxPreference){
if (numeronotifiche.equals("3")){
alarm.CancelAlarm(this);
alarm.SetAlarm3(this);
}
else if (numeronotifiche.equals("1")){
alarm.CancelAlarm(this);
alarm.SetAlarm1(this);
}
else if (numeronotifiche.equals("2")){
alarm.CancelAlarm(this);
alarm.SetAlarm2(this);
}
else {
//
}
}
else {
//
GestioneAllarme alarm2 = new GestioneAllarme();
alarm2.CancelAlarm(this);
}
//
if (Sound.equals("")){
Sound = "2";
Editor editor = prefs.edit();
editor.putString("sound", "2");
editor.commit();
}
if (ListPreference.equals("")){
ListPreference = "1500";
Editor editor = prefs.edit();
editor.putString("listpref", "1500");
editor.putInt("indexfade", 1500);
editor.commit();
}
if (numeronotifiche.equals("")){
numeronotifiche = "2";
Editor editor = prefs.edit();
editor.putString("numeronotifiche", "2");
editor.commit();
}
fade = Integer.parseInt(ListPreference);
notify = Integer.parseInt(numeronotifiche);
if (fade == 500){
animazione = R.style.MyCustomTheme1;
fadein = R.anim.fadein500;
fadeout = R.anim.fadeout500;
}
else if (fade == 1000){
animazione = R.style.MyCustomTheme2;
fadein = R.anim.fadein1000;
fadeout = R.anim.fadeout1000;
}
else if (fade == 1500){
animazione = R.style.MyCustomTheme3;
fadein = R.anim.fadein1500;
fadeout = R.anim.fadeout1500;
}
else if (fade == 2000){
animazione = R.style.MyCustomTheme4;
fadein = R.anim.fadein2000;
fadeout = R.anim.fadeout2000;
#Override
protected void onResume() {
super.onResume();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
preferencesChanged = true;
}
};
sp.registerOnSharedPreferenceChangeListener(listener);
protected void onStop(){
super.onStop();
if(preferencesChanged){
//Update the app
preferenze();
}
}
public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.preferences);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String listpref) {
It seems that as soon as preferenze() always modify the shared preferences you will have an infinite loop.
Since you didn't post the complete code it's difficult to say. But I guess that your code is such that it always modify prefs only on android 2.x
you can try something like this to avoid infinite loop.
private boolean isPreferenzeRunning = false;
...
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String listpref) {
if(!isPreferenzeRunning)preferenze();
}
};
...
private void preferenze()
isPreferenzeRunning = true;
try{
...
}finally{isPreferenzeRunning = false;}
}
That code wouldn't even compile.
The code in preferenze() will return the preference values (boolean, String, int, etc), not the Preference objects. By changing the values in that method, you will also cause a StackOverflowError.
What is the need for a OnSharedPreferenceChangeListener?
// here several if/if else to change value
Those sentences probably change the Shared Preferences, that in turn will fire your listener, which in turn will call preferenze, ..., and so on. If this continues for ever, a S.O will be thrown. Now depending on the conditions it could happen that the preferenze method only reads but does not modify anything. In this case the loop will end.
And about the error being observed in 2.X devices only, it could be due to 4.x devices being more recent and probably having more RAM memory.
UPDATE:
The code is still incomplete. Looks like there are two activities: the one you posted first and the new one. I guess (that's all I can do with the code you posted) you have a PreferenceActivity to show the settings and allow the user to change them, and the listener is there to update other parts of the application according to the new settings. The problem is that when the listener is called, it itself modifies the settings, and this in turn will call the listener again, which will modify the preferences again, and so on. This will throw an SOException once the heap runs out of memory.
A way of rearranging the code to solve this would be:
Register the OnSharedPreferenceChangeListener in your activity's onResume instead of onCreate, and deregister it in the onPause method (calling unregisterOnSharedPreferenceChangeListener). Deregistering is very important because we don't want to listen for changes once the user leaves the screen, or the activity is recreated by the system (for instance when the device rotates):
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//We have removed the listener registration from here
}
#Override
protected void onResume() {
super.onResume();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String listpref) {
//I'll show what to do here in point 2.
}
};
sp.registerOnSharedPreferenceChangeListener(listener);
}
#Override
protected void onPause() {
super.onPause();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.unregisterOnSharedPreferenceChangeListener(listener);
}
With your current code, each time the user changes a single setting, the preferenze method is called to update the app. So if it changes 5 fields, the method is called 5 times. What we could do now is to check for changes just once. I assume you don't care how many fields the user has changed, since all you need is to know if there are changes or not. So in the listener, instead of calling preferenze, you could set a boolean flag to true:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
preferencesChanged = true;
}
Ok, so now we have a way of telling whether the settings have changed or not. When the user is done and the activity is about to be closed, the methods onPause, onStop and onDestroy will be called in this order. You can use one of these methods to check the boolean flag and only if there are changes, update the app. This way, if the user changes 1, 3 or 20 fields, we will update the app just once at the end. You can do this in any of the 3 thethods, but it's very important to do this AFTER deregistering the listener (onPause), or else you'll run into problems again. Example:
protected void onStop(){
super.onStop();
...
if(preferencesChanged){
//Update the app
preferenze();
}
}
You might need to change some things but overall you get the idea.

Categories