I am going crazy trying to figure out how to store a variable someplace so when I switch to another activity then back again the variable still contains this value. I did this a while ago using shared preferences, but this was a bad solution, this was way more permanent than I needed and just not the correct way.
If a certain button is pressed in activity one, this opens activity two via an intent and sets a value to a string in activity two. When going to another activity, or pressing the back button, then returning to activity two, the string is reset back to its initial value.
I have tried:
Shared Preferences (Worked but not good) Static variables (Did not seem to make any difference, maybe I am doing something wrong) Using saved instance state and restore state methods like so:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString(destination, des);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
des = savedInstanceState.getString(destination);
}
I am at a point where I admit I have no idea how to do this, and Frankensteining code together is causing me problems than anything else, but so far no pitchforks on the horizon.
In order to save a value inside a string between activities, what do you experienced programmers recommend?