I know that I can pass some values between Activities using intent.
However, if I want to pass whole Activity to another Activity I think it is not good approach.
Is there another way to do that?
I have Settings Activity in which I am changing some Colors. So after I come back to my Main Activity I would like to apply those colors. To do this, I need access to MainActivity fields after I change Color value, so inside PreferenceActivity. In other words, I want to have access to Main activity fields from PreferenceActivity class. Any ideas?
Shared preferences can be used. If you want your changes to be reflected right away add listener. Refer to SharedPreferences.onSharedPreferenceChangeListener. Its an easy way to do.
If you want to lots of changes required in many activity from you change in any one.
And access last modify data from all Activity and modify also.
for example.
Constants.java
In your MainActivity you have an editText.
MainActivity.java
In your Activity2 you have an TextView and that getting value which you enter in MainActivity.java without pass in Intent.
Activity2.java
like that you use many values from all activity and modify from all activity.
I think you could pass the value by using method putExtra(name, value). And after you start new activity you can get the value you pass before by using method getStringExtra(name).
You should be using a
SharedPreference
and then accessing that in your main activity. I recommend you reading up at http://developer.android.com/guide/topics/ui/settings.html because it seems like you are implementing your settings activity incorrectly. The part you might be specifically interested in is the "Read Preferences" section. However, I strongly suggest you read through the whole thing and then implement your settings the proper way.Updated answer with the 3 different ways (that I can think of):
1) Start your preference activity using
startActivityForResult()
, then in youronActivityResult()
access theSharedPreference
and make your necessary changes. See here2) Register a
SharedPreferenceChangeListener
with your MainActivity, which will be called when any changes happen to yourSharedPreference
. See here for a detailed discussion. Also see my initial response.3) In your MainActivity's
onResume()
, access theSharedPreference
and then make your changes there. I do not like this method because you will be clutteringonResume()
with more logic and you will also probably have to have a variable that keeps track of the state of the variable you are interested in.I would personally go with option 2 because the callback was created for this exact purpose.