如何在Fragments中使用setArguments()和getArguments()方法?


98

我有2个片段:(1)Frag1(2)Frag2。

片段1

bundl = new Bundle();
bundl.putStringArrayList("elist", eList);

Frag2 dv = new Frag2();
dv.setArguments(bundl);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.the_fragg,dv);
ft.show(getFragmentManager().findFragmentById(R.id.the_fragg)); 
ft.addToBackStack(null);
ft.commit();

如何在Frag2中获取此数据?

Answers:



192

只需调用getArguments()Frag2onCreateView()方法:

public class Frag2 extends Fragment {

     public View onCreateView(LayoutInflater inflater,
         ViewGroup containerObject,
         Bundle savedInstanceState){
         //here is your arguments
         Bundle bundle=getArguments(); 

        //here is your list array 
        String[] myStrings=bundle.getStringArray("elist");   
     }
}

12
就我而言,它返回null,为什么会这样呢?
Anirudh 2013年

2
您将ArrayList放入包中,但是得到了一个String Array。你应该做的bundle.getStringArrayList("elist");
拉法尔

1
您忘记了返回语句:return super.onCreateView(inflater, container, savedInstanceState);
user41805 '16

4
一直在调用oncreateview。因此,只需在oncreate()方法中调用getarguments。仅当片段被销毁或新创建的时间时才会调用。
Mohamed Ibrahim

5
@almaz_from_kazan @HabeebPerwad为什么getArguments()onCreateViewin中使用,而不在in中使用onCreate
Nik Kober

38

例如:添加数据:-

   Bundle bundle = new Bundle();
   bundle.putString("latitude", latitude);
   bundle.putString("longitude", longitude);
   bundle.putString("board_id", board_id);
   MapFragment mapFragment = new MapFragment();
   mapFragment.setArguments(bundle);

例如:获取数据:-

String latitude =  getArguments().getString("latitude")

7

在Frag1中:

Bundle b = new Bundle();

b.putStringArray("arrayname that use to retrive in frag2",StringArrayObject);

Frag2.setArguments(b);

在Frag2中:

Bundle b = getArguments();

String[] stringArray = b.getStringArray("arrayname that passed in frag1");

就这么简单。


5

以正确的方式实例化片段!

getArguments() setArguments()当使用静态方法实例化Fragment时,这些方法似乎非常有用。
Myfragment.createInstance(String msg)

怎么做?

片段代码

public MyFragment extends Fragment {

    private String displayMsg;
    private TextView text;

    public static MyFragment createInstance(String displayMsg)
    {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.setString("KEY",displayMsg);
        fragment.setArguments(args);           //set
        return fragment;
    }

    @Override
    public void onCreate(Bundle bundle)
    {
        displayMsg = getArguments().getString("KEY"):    // get 
    }

    @Override
    public View onCreateView(LayoutInlater inflater, ViewGroup parent, Bundle bundle){
        View view = inflater.inflate(R.id.placeholder,parent,false);
        text = (TextView)view.findViewById(R.id.myTextView);
        text.setText(displayMsg)    // show msg
        returm view;
   }

}

假设您要在创建实例时传递字符串。这就是您将要做的。

MyFragment.createInstance("This String will be shown in textView");

阅读更多

1) 为什么Myfragment.getInstance(String msg)优于new MyFragment(String msg)?
2) 片段上的示例代码


android.os.Bundle没有setString。你是说putString()吗?
Stealth Rabbi

2

对于像我这样希望发送除基元以外的对象的用户,由于您无法在片段中创建参数化的构造函数,因此只需在片段中添加一个setter访问器,这对我总是有用的。


那是错误的方式。如果片段重新创建,它将丢失那些参数。发送到片段的参数应可序列化,并通过传递setArguments()。可以使用setter设置不可序列化的参数,但是在重新创建活动/片段时应再次调用它。
CoolMind
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.