Pass reference of one Activity to another Activity

2019-06-24 11:41发布

问题:

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?

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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.