简体   繁体   English

在Fragment中使用MainActivity中的值

[英]using values from MainActivity in Fragment

My app has a bottomNavigationBar with three fragments. 我的应用程序具有一个带有三个片段的bottomNavigationBar。 My MainActivity contains a CollapsingToolbar with a RadioGroup inside. 我的MainActivity包含一个CollapsingToolbar,其中包含一个RadioGroup。 Now I get the value of the selected RadioButton in the MainActivity, but I need the value to work with it inside my first fragment. 现在,我在MainActivity中获得了选定的RadioButton的值,但是我需要在第一个片段中使用它的值。 How do I do that? 我怎么做? Does every fragment contains its own CollapsingToolbar or is the data passed between the activities? 每个片段都包含其自己的CollapsingToolbar还是活动之间传递数据?

  1. You can use the fragment's setArguments(Bundle) method where the Bundle has key-value pairs that you've set. 您可以使用片段的setArguments(Bundle)方法,其中Bundle具有已设置的键值对。 For example, your fragment object is yourFragment then you have 例如,您的片段对象是yourFragment那么您有

     Bundle bundle = new Bundle(); bundle.putString("paramKey", "paramVal"); yourFragment.setArguments(bundle); 
  2. In the fragment's onCreateView(LayoutInflater, ViewGroup, Bundle) you can access the information with the getArguments() method. 在片段的onCreateView(LayoutInflater, ViewGroup, Bundle) ,可以使用getArguments()方法访问信息。

     String value = getArguments().getString("paramKey"); // value = "paramVal" // inflate, return 

Have a look at the documentation for more information on setting bundle values. 请参阅文档 ,以获取有关设置包值的更多信息。

You can use put the values you want to pass in SharedPreferences 您可以使用想要传递给SharedPreferences的值

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor edit = prefs.edit();
    edit.putString("some_key",someValue); //someValue is a var that containns the value that you want to pass
    edit.commit();

Then in your fragment, access the value: 然后在您的片段中,访问值:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String value = prefs.getString("some_key","default_value");

Another rather less efficient way 另一种效率较低的方法

Create a Utility class that will hold all your static variables. 创建一个将包含所有静态变量的Utility类。 You will be able to set and get the values of these variables in all instances of that class 您将能够在该类的所有实例中设置并获取这些变量的值。

This can also be achieved using Java interfaces like this 也可以使用这样的Java接口来实现

  1. Have an Interface defined in your Activity class. 在Activity类中定义一个接口。 Capture the interface instance while committing your fragment which will later be used to send data to fragment 在提交片段时捕获接口实例,该片段随后将用于将数据发送到片段

.

  class ExampleActivity extends Activity {

        //Data listener to be implemented by the fragment class
        public interface OnDataListerner{
            public void sendData(ArrayList<String> data);
        }

        //DataListener instance to be captured while committing fragment
        OnDataListener fragment;

        //commit your fragment and type cast it to OnDataListener
        private void commit Fragment(){
           fragment = new ExampleFragment();
           FragmentTransaction transaction = 
           getSupportFragmentManager().beginTransaction();
           transaction.replace(R.id.fragment_container, exampleFragment);
           transaction.addToBackStack(null);
           transaction.commit();
        }

        //used to send data through interface methods which will be defined in fragment
        public void sendDataToFragment(){
           fragment.sendData(Your data to be send);
        }

 }
  1. Have this interface implemented in your Fragment class and once Acitivity calls any method on this interface it will be called in this Fragment 在您的Fragment类中实现此接口,一旦Acitivity在此接口上调用任何方法,它将在此Fragment中调用

    public class ExampleFragment extends Fragment implements ExampleActivity.OnDataListerner { 公共类ExampleFragment扩展Fragment实现ExampleActivity.OnDataListerner {

     //interface callback which is called when Activity call its method. public void sendData(ArrayList<String> data){ //Here is your data which can be consumed } 

    } }

Hope this helps. 希望这可以帮助。

You can create a method in the fragments visible to the activity class. 您可以在活动类可见的片段中创建一个方法。 Then using this method, you can pass the values. 然后,使用此方法,可以传递值。 And in implementation of this function, you can do required changes such as UI updates in the fragment. 并且在实现此功能时,您可以进行所需的更改,例如片段中的UI更新。

For example - 例如 -

MainActivity.java MainActivity.java

// in member declarations
private MyFragment frag;
private CentralFragment cFrag;

// initialize the fragment
frag = new MyFragment(args);
cFrag = new CentralFragment(otherArgs);

// onRadioButtonClicked -> assuming selected value = v
frag.onChoice(v);
cFrag.onChoice(v);

MyFragment.java MyFragment.java

public void onChoice(Type arg) {
    // use the value 
    someTextView.setText(arg);
}

CentralFragment.java CentralFragment.java

public void onChoice(Type arg) {
    // use the value 
    otherTextView.set(arg);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM