Shared Preferences won't save the data - java

I'm new to Android, and a bit to Java (Don't ask)
I want to use the Shared Preferences, and till to this day everything worked fine, but in my new project the shared preferences won't save the data.
In my onCreate Method I have following code:
preferences = this.getSharedPreferences(KEY, MODE_PRIVATE);
editor = preferences.edit();
preferences.getFloat(TESTEPFLOAT, 0);
preferences.getFloat(TESTCURRENTEPFLOAT, 0);
preferences.getInt(TESTLEVEL, 1);
in my onDestroy Method:
#Override
public void onDestroy(){
super.onDestroy();
editor.putInt(TESTLEVEL, level);
editor.putFloat(TESTEPFLOAT, ep);
editor.putFloat(TESTCURRENTEPFLOAT, currentEP);
editor.apply();
}
I don't know where the problem is, and I hope someone can help me

Don't use onDestroy(). There's no guarantee it will get called. If you put a break point in that method, my guess is it's not getting called when you think it is.

change to this:
preferences = this.getSharedPreferences(KEY, MODE_PRIVATE);
preferences.getFloat(TESTEPFLOAT, 0);
preferences.getFloat(TESTCURRENTEPFLOAT, 0);
preferences.getInt(TESTLEVEL, 1);
to save your data:
#Override
protected void onPause() {
super.onPause();
preferences = this.getSharedPreferences(KEY, MODE_PRIVATE);
editor = preferences.edit();
editor.putInt(TESTLEVEL, level);
editor.putFloat(TESTEPFLOAT, ep);
editor.putFloat(TESTCURRENTEPFLOAT, currentEP);
editor.apply();
}
or use editor.commit(); to save data immediately

Just as #Gavin Wright said, onDestroy don't get called everytime. Move the code to onPause(). onPause() is guaranteed to get called everytime you navigate away from your activity.
#Override
public void onPause(){
super.onPause();
editor.putInt(TESTLEVEL, level);
editor.putFloat(TESTEPFLOAT, ep);
editor.putFloat(TESTCURRENTEPFLOAT, currentEP);
editor.apply();
}

Related

Saving ImageView background state

I have an ImageView ImageView1, whose background can be set by the user. How can I save the state of that background, so that when the user closes the app and reopens it, the background of ImageView1 is still the same?
Is it possible with onSaveInstanceState or do I have to write something different, and if so: How can I do this?
Right now I have:
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("ImageView1", imageView1);
but imageView1 gets red underline, even though declared before to id of ImageView
You can use shared preferences:
Save to shared preferences on change.
Get from shared preferences on app reopen.
Check documentation for more:
https://developer.android.com/training/data-storage/shared-preferences
You can use SharedPreferences as follows:
SharedPreferences sharedPreferences;
sharedPreferences = getSharedPreferences("data", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("image", //put image path here);
editor.apply();
Then override the onResume method as follows
#Override
protected void onResume() {
super.onResume();
SharedPreferences sharedPreferences = getSharedPreferences("data", 0);
String imagePath = sharedPreferences.getString("image", ""); //default value is empty
}
Then load image using picasso or glide like..
Picasso.get().load(imagePath).into(imageview);

How to call a method just before an app is closed?

I have a variable who contains data that can change when user is using app.
My goal is to store those data in SharedPrefs just before user is closing app,
I was thinking about something like
OnStop() {
Preferences.edit()... }
but i don't know the correct syntax ...
thanks in advance guys
Save your data in SavedPrefs in onStop Method.
Syntex will be like
In onStop Method
#Override
protected void onStop() {
super.onStop();
SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = saved_values.edit();
edit.putString("key", "stringToBeSaved")
editor.commit();
}
And then to retrive your data in onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences saved_values = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String foo = saved_values.getString("key", "default_string");
}
Remember the key should be same.
For more info check out Android docs on
Shared Preferences: Save key-value data
Android Lifecycle: Understand the Activity Lifecycle
Activity.onStop() is called just before the app is completely closed (kicked from recents)
You should probably use Activity.onPause() to save your preferences as it will also be called when the user just switches to another app.
Further information:
https://developer.android.com/guide/components/activities/activity-lifecycle
You should save your persistent data in onPause() rather than in onStop().
Then you can retrieve your data in onResume().
For saving the data you can take a look at this example
SharedPreferences.Editor editor = getSharedPreferences("myPrefs", MODE_PRIVATE).edit();
editor.putString("username", "My name");
editor.apply();
And then retrience your data like this
SharedPreferences prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
String username = prefs.getString("username", "No username"); //No username is the default value

Why are my SharedPreferences persisting across activites after using clear() in Android?

I've looked all over and can't seem to find an answer to my issue. For some reason the data saved in SharedPreferences reappears after switching activities. Why is my SharedPreferences still the exact same as it was before I cleared it and how can I fix this?
In first activity:
#Override
protected void onStop() {
super.onStop();
SharedPreferences sp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
Gson gson = new Gson();
String objectJson = gson.toJson(object, Object.class);
String activityJson = "FirstActivity";
SharedPreferences.Editor editor = sp.edit();
editor.putString(CONTINUE_KEY, objectJson);
editor.putString(ACTIVITY_KEY, activityJson);
editor.apply();
}
public void onClickSave() {
Gson gson = new Gson();
String objectJson = gson.toJson(object);
SharedPreferences sp getSharedPreferences(SAVE_KEY, MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(object.getDate(), objectJson);
editor.apply();
SharedPreferences spTemp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
spTemp.edit().clear().apply();
// go back to the main menu
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
Before the apply():
After the apply():
In Main Activity:
SharedPreferences sp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
From main activity:
From Coordinating activities android documentation.
When one activity starts another, they both experience lifecycle
transitions. The first activity stops operating and enters the Paused
or Stopped state, while the other activity is created. In case these
activities share data saved to disc or elsewhere, it's important to
understand that the first activity is not completely stopped before
the second one is created. Rather, the process of starting the second
one overlaps with the process of stopping the first one.
The order of lifecycle callbacks is well defined, particularly when
the two activities are in the same process (app) and one is starting
the other. Here's the order of operations that occur when Activity A
starts Activity B:
Activity A's onPause() method executes.
Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
Then, if Activity A is no longer visible on screen, its onStop() method executes.
This predictable sequence of lifecycle callbacks allows you to manage
the transition of information from one activity to another.
Back to your case, when you start MainActivity from FirstActivity.
FirstActivity's onPause() method executes.
MainActivity's onCreate(), onStart(), and onResume() methods execute in sequence. (MainActivity now has user focus.)
Then, if FirstActivity is no longer visible on screen, its onStop() method executes.
When the code in onClickSave executes.
SharedPreferences spTemp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
spTemp.edit().clear().apply();
At this time, TEMP_KEY prefs has been cleared. After MainActivity is visible on screen, FirstActivity will be no longer visible on screen, so its onStop() method executes.
SharedPreferences sp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
Gson gson = new Gson();
String objectJson = gson.toJson(object, Object.class);
String activityJson = "FirstActivity";
SharedPreferences.Editor editor = sp.edit();
editor.putString(CONTINUE_KEY, objectJson);
editor.putString(ACTIVITY_KEY, activityJson);
editor.apply();
In this code you add two entries into TEMP_KEY prefs again. So when users click on a button in MainActivity, at that time the prefs size is 2 instead of 0 as you expected.
Why is my SharedPreferences still the exact same as it was before I
cleared it?
This is predictable or expected behavior in Android. You can put a log in onCreate(), onStart(), onResume() method of MainActivity, then you can see TEMP_KEY prefs size at that time is always 0.
How can I fix this?
Do not add new entries into TEMP_KEY in onStop(), you should add in onPause() instead.
Save To Shared Preferences
SharedPreferences sp = getSharedPreferences("test" , MODE_PRIVATE);
SharedPreferences.Editor spEditor = sp.edit();
spEditor.putString("hello1" , "hello11");
spEditor.putString("hello2" , "hello22");
spEditor.apply();
Clear From Shared Preferences
SharedPreferences sp1 = getSharedPreferences("test" , MODE_PRIVATE);
SharedPreferences.Editor spEditor1 = sp1.edit();
spEditor1.clear();
spEditor1.apply();
Get From Shared Preferences
SharedPreferences sp2 = getSharedPreferences("test" , MODE_PRIVATE);
String value = sp2.getString("hello1" ,"noting");
Log.i("test_sp" , " == == ==" +value);
First, you have different preference file name for getSharedPreferences(String name, int mode), in onStop() and onClickSave() methods:
// in onStop()
SharedPreferences sp = this.getSharedPreferences(TEMP_KEY, MODE_PRIVATE);
// in onClickSave()
SharedPreferences sp getSharedPreferences(SAVE_KEY, MODE_PRIVATE);
you need to use the same file name to access the same preference.
Second, onStop() is not always guaranteed to be called, so it's not a reliable method to save your preferences. Related answer: Is onStop() always preceded by onPause()
Third, always save the preference when you need to keep the value in it. Don't wait to save it later and hoping that you can depend on either onPause(), onStop(), or onDestroy(). Never assume that your application will gracely terminate by user or system.

How to remember score after app has been restarted?

I am making a two player game where I am able to have a rematch. I am also keeping track of the scores of each player. This is what my code for a rematch looks like:
public void rematch(View view) {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(i);
}
The above code is called when the user clicks a rematch button. I basically just restart the application because it's easier than resetting all the variables I have. Anyways, to keep track of the scores, I am overriding the onStop() method like this:
#Override
protected void onStop() {
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(SCORES_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("scoreOne", p1Score);
editor.putInt("scoreTwo", p2Score);
editor.commit();
}
I am using shared preferences. Then, in my onCreate method, I get these shared preferences like this:
// Remember scores from previous games
SharedPreferences settings = getSharedPreferences(SCORES_NAME, 0);
p1Score = settings.getInt("scoreOne", 0);
p2Score = settings.getInt("scoreTwo", 0);
updateScores();
SCORES_NAME is
public static final String SCORES_NAME = "MyScores";
This all isn't working. My scores after the game restarts are always back to 0. I was thinking of putting extras onto the intent in rematch() but I'm not sure how to access this intent in my onCreate() method. Any help is greatly appreciated!
You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putInt("scoreOne", p1Score);
savedInstanceState.putInt("scoreTwo", p2Score);
// etc.
}
The Bundle is essentially a way of storing a NVP ("Name-Value Pair") map, and it will get passed in to onCreate() and also onRestoreInstanceState() where you'd extract the values like this:
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
int p1Score = settings.getInt("scoreOne", 0);
int p2Score = settings.getInt("scoreTwo", 0);
}
You would usually use this technique to store instance values for your application (selections, unsaved text, etc.).
Problem here is that you made the assumption that the new activity's onCreate() will be called after your first activity's onStop() has completed execution.
That's probably not an assumption you want to make, because they are two not completely related things.
Send the values you need to the newly created activity in the intent
public void rematch(View view) {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Those next values will be there for sure in the new activity.
i.putExtra("scoreOne", p1Score);
i.putExtra("scoreTwo", p2Score);
finish();
startActivity(i);
}
And in the onCreate of your Activity can extract those values as follows
public void onCreate(Bundle savedInstance) {
super.onCreate();
..
int p1Score = getIntent().getIntExtra("scoreOne", 0);
int p2Score = getIntent().getIntExtra("scoreTwo", 0);
}
You need to take the code from onStop and execute it before your call to finish() from the rematch method. Like this:
private void saveScores() {
SharedPreferences settings = getSharedPreferences(SCORES_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("scoreOne", p1Score);
editor.putInt("scoreTwo", p2Score);
editor.commit();
}
And then call this function from rematch:
(...)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
saveScores();
finish();
startActivity(i);
However, #elmorabea's solution is better is you are now only using SharedPreferences to pass values between activities.

Google Maps API Reload Markers

I add Markers in the Google Map API with the following code
googleMap.addMarker(new MarkerOptions()
.position(point)
.title(text)
.snippet(textinfo)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}
How can I make them save (I guess in onResume and in OnPause) so they will appear when i re-launch the app and not dissapear?
I don't think there is any way to automatically save the markers between launches of the map, so you either have to save the info to recreated it, or you could make use of the fact that googleMap.addMarker() returns a Marker object, and serialize that to SharedPerferences and then re-add it the next time your app launches.
See the following:
addMarker()
and
Marker
For an example of using Shared Preferences see the following link, but here is the example from the developers guide.
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
#Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}

Categories