Reputation: 13
Such as if we change the background color by clicking the button and the code to change background color is:
relativeLayout.setBackgroundColor(Color.parseColor("#03a9f4"));
It will change the background to blue. But it will turn to default white after switching the activities or closing the activities. I want the color to be constant after changing it. Can anyone help me?
Upvotes: 1
Views: 42
Reputation: 6126
You can get the last color you used in your designated View (let's call it view
) and store it in SharedPreferences
in onPause()
:
int lastColor = ((PaintDrawable) view.getBackground()).getPaint().getColor();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity);
pref.edit().putInt("last_color", lastColor).commit();
Then restore the color in onResume()
:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity);
int lastColor = pref.getInt("last_color",0);
view.setBackgroundColor(lastColor);
Upvotes: 1
Reputation: 7749
Persist the color (e.g. in SharedPreferences
) and then set it everytime your View
is created.
Upvotes: 0