Best practice for instantiating a new Android Fragment

前端 未结 13 1469
暖寄归人
暖寄归人 2020-11-21 04:38

I have seen two general practices to instantiate a new Fragment in an application:

Fragment newFragment = new MyFragment();

and

<         


        
13条回答
  •  逝去的感伤
    2020-11-21 05:20

    use this code 100% fix your problem

    enter this code in firstFragment

    public static yourNameParentFragment newInstance() {
    
        Bundle args = new Bundle();
        args.putBoolean("yourKey",yourValue);
        YourFragment fragment = new YourFragment();
        fragment.setArguments(args);
        return fragment;
    }
    

    this sample send boolean data

    and in SecendFragment

    yourNameParentFragment name =yourNameParentFragment.newInstance();
       Bundle bundle;
       bundle=sellDiamondFragments2.getArguments();
      boolean a= bundle.getBoolean("yourKey");
    

    must value in first fragment is static

    happy code

提交回复
热议问题