4

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?

Squonk
  • 48,735
  • 19
  • 103
  • 135
Marek
  • 3,935
  • 10
  • 46
  • 70

4 Answers4

3

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 your onActivityResult() access the SharedPreference and make your necessary changes. See here

2) Register a SharedPreferenceChangeListener with your MainActivity, which will be called when any changes happen to your SharedPreference. See here for a detailed discussion. Also see my initial response.

3) In your MainActivity's onResume(), access the SharedPreference and then make your changes there. I do not like this method because you will be cluttering onResume() 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.

Community
  • 1
  • 1
btse
  • 7,811
  • 3
  • 25
  • 30
  • I already read it, but I want changes to take effect straight away after coming back from PreferenceActivity – Marek Jul 05 '13 at 04:19
0

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

Angripa
  • 159
  • 2
  • 4
  • 14
  • 6
    @Marek : **"I want to pass whole Activity..."** - No, don't attempt to do this. Save the changes to the preferences and in the `MainActivity` simply reload the changes in `onResume()`. Either that or use `startActivityForResult(...)` which allows you to pass data back to a calling `Activity` in a `Bundle`. – Squonk Jul 05 '13 at 03:19
  • @Squonk I did what you suggested. Indeed I just realized that passing whole activity is a bad idea. However, do you think that in some case it could be better? For example if I would like to change a lot of data? Please put your comment as an answer, it satisfies me the most so I would like to accept it. – Marek Jul 05 '13 at 05:22
0

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.

Venkatesh
  • 3,558
  • 8
  • 32
  • 38
-1

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

public class Constants
{

    public static String name;
}

In your MainActivity you have an editText.

MainActivity.java

public class MainActivity extends Activity {

    private EditText yourName;
    private Button btn;

     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    yourName = (EditText) findViewById(R.id.yourName);

    btn = (Button) findViewById(R.id.btn);

    btn.setOnClickListener(new OnClickListener()
    {       
        public void onClick(View v)
        {

            Constants.name = yourname.getText().toString();
            Intent intent = new Intent(getApplicationContext(),Activity2.class);
            startActivity(intent);

        }
    });

}

In your Activity2 you have an TextView and that getting value which you enter in MainActivity.java without pass in Intent.

Activity2.java

public class Activity2 extends Activity {

    private TextView yourName;

     @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    yourName = (TextView) findViewById(R.id.tv_yourName);

    // directly use ferom serializable class
    yourname.setText(Constants.name);
}

like that you use many values from all activity and modify from all activity.

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
  • I think in the example that you presented Constants class does not need to be Serializable. – Marek Jul 05 '13 at 08:45
  • Ya... for just use values from Activities it not need to Serializable... but it works easy compare to another way... – Mr.Sandy Jul 05 '13 at 11:22