0

I have two activities. One is main activity and another is settings activity. In settings activity I am setting a color variable.

This I want to access in main activity as default color which is set in settings activity.

I tried to declare color as static variable and access it in another activity. But the problem is if I directly open main activity I don't get the color as it dose not get set in settings activity.

How to save this variable in settings activity? I want to save color as a default for events in settings activity.

Thank you.

Edit :

I tried this using shared preferences but still i don't get default color in main activity.

In settings:

 ((GradientDrawable)selectColor.getBackground()).setColor(Color.BLUE); mColor = Color.BLUE; SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt("color",mColor); editor.commit(); 

In main activity:

 SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE); mColor = sharedPref.getInt("color",0); 

3 Answers 3

3

Since you're saying from a settings activity, I assume you have multiple settings and you want them to be sticky (you want them to be remembered for future times you run the app)? If so, use shared preferences to store the color you want it to be, and in onResume of your main activity read the preference and change the color accordingly.

If you don't want them to be sticky, call startActivityForResult to launch the settings activity, and return the settings as the result. Then the main activity should override onActivityResult to get the results.

Sign up to request clarification or add additional context in comments.

2 Comments

but, so it will be necessary to set the color in settings first and then go to another activity? I have set default color in settings. Can't i access this without going to settings?@Gabe Sechan
When you read settings, you can provide a default. The read function will return this value if the setting hasn't been written yet. So you can just provide the default to that function and it will be used until your settings activity writes it.
0

save color hex code or color name in the Shared Preferences

Comments

0

You have 2 options for that :

1- As what Gabe Sechan said, you should use startActivityForResult

 startActivityForResult(intent, 2);// Activity is started with requestCode 2 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // check if the request code is same as what is passed here it is 2 if(requestCode==2) { String message=data.getStringExtra("MESSAGE"); textView1.setText(message); } } 

2- You can use Callback methods to handle your listener when you change the color.

Both of them are working with me as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.