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

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

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);

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.

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();
}
}

SharedPreferences not updating value when returning to Activity, but only keeps initial Activity load value

So I am trying to use SharedPreferences to pass a value from the Activity to a Class. The problem is the following:
On initial loading of the Activity, the value is stored in SharedPreferences but if I exit the Activity and return back, and try to update the value, the SharedPreferences does not seem to update and stays at the value of the initial load of the Activity.
Some class function
private void populatePlayerQueue(){
int category_index;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getContext());
category_index = sharedPreferences.getInt("selected_category", 0);
}
Activity onResume
#Override
public void onResume() {
super.onResume();
int tmp = Integer.parseInt(getIntent().getStringExtra("CategoryIndex"));
//store the category in the shared preferences
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("selected_category", tmp);
editor.commit();
}
What I am trying to do:
I have to send an integer from the activity to a specific class and if I come back to the activity then I need to store the update value and so the class will be able to access the newly updated value again. I only have access to the context and not the activity. This causes errors if I try to create an interface callback to communicate with the activity. So this option is eliminated, hence I am trying anything I can to do that.
For some better result remove the SharedPreference through its key at the time of exit application.
In your case when you exit your application just clear the SharedPreference like:
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("selected_category"); //Just remove it by its specific key
editor.commit();
Then it will clearly load and save the new value again when you go back to your application on the same key or you can use new one.

Reload SharedPreferences on resume? (or how to refresh/reload activity)

How can I reload SharedPreferences when I resume from one activity to another? If I resume, it is possible that user has changed the settings. Is it possible to reload SharedPreferences or do I need to refresh/reload activity. And if, then how?
There is no difference in how you get and set SharedPreferences normally and from doing so in onResume. What you will need to do in addition to getting the most recent preferences, is update any objects you have in the Activity that use preference values. This will ensure your Activity is working with the most recent values.
A simple example:
protected void onResume() {
super.onResume();
getPrefs();
//...Now update your objects with preference values
}
private void getPrefs() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String myPref = prefs.getString("myPref", "");
}

Categories