简体   繁体   中英

Pass Data from Activity to Fragment in Viewpager Tablayout

I used below code to passing data from Activity to Fragment.

Fragment Code

public class StoryDetailFragmentInfo extends Fragment {
View view;
TextView txtDescrible;

final static String getData = "abc";

public static StoryDetailFragmentInfo newInstance(String paramater){
    Bundle bundle = new Bundle();
    bundle.putString(getData,"Message");
    StoryDetailFragmentInfo fragmentInfo = new StoryDetailFragmentInfo();
    fragmentInfo.setArguments(bundle);
    return fragmentInfo;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(getArguments()!=null){
        String test = getArguments().getString(getData);
    }



}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.story_detail_fragment_info_layout, container, false);

     txtDescrible = view.findViewById(R.id.txt_describleDetail);

    return view;
}

}

MainActivity Code

   @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.story_detail_layout);
    addControls();
    loadData();
    intentData();
    fillDataToView();
}

private void intentData() {
    StoryDetailFragmentInfo.newInstance("Hello babe");

}

Problem is getArgument() in my fragment is null and i can't get data What should i do?
Can you help me explain it? Thanks for reading.

  1. If you want to pass data from activity to fragment as initial data, you can send data from activity to ViewPagerAdapter, then pass data from ViewPagerAdapter to fragment in getItem() function. In Activity:

    adapter = new ViewPagerAdapter(getSupportFragmentManager(), this,false)); adapter.setData(data);

In ViewPagerAdapter:

    @Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            MyFragment fragment = new MyFragment();
            fragment.setData(data);
            return fragment
    }
}
  1. If you want to pass data from activity to fragment in real time (like updating data), I recommend to use EventBus. It is a modern and convenient tool to communicate between activity and fragment. Check it at https://github.com/greenrobot/EventBus

Firstly, in the method newInstance of class StoryDetailFragmentInfo , you write these: bundle.putString(getData,"Message"); while leave paramater alone. So you need change

 bundle.putString(getData,"Message"); 

to

 bundle.putString(getData,paramater);

Secondly, you try to get values from arguments from fragment by the key 'AdapterStoryDetailViewPager.idData', but this key is different from the key which you used to save value. So you need to change

String test = getArguments().getString(AdapterStoryDetailViewPager.idData);

to

String test = getArguments().getString(getData);

After these two steps, you would get the right value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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