Send data from activity to fragment in Android

2018-12-31 01:05发布

I have two classes. First is activity, second is a fragment where I have some EditText. In activity I have a subclass with async-task and in method doInBackground I get some result, which I save to variable. How can I send this variable from subclass "my activity" to this fragment?

19条回答
春风洒进眼中
2楼-- · 2018-12-31 01:28

Basic Idea of using Fragments (F) is to create reusable self sustaining UI components in android applications. These Fragments are contained in activities and there are common(best) way of creating communication path ways from A -> F and F-A, It is a must to Communicate between F-F through a Activity because then only the Fragments become decoupled and self sustaining.

So passing data from A -> F is going to be the same as explained by ρяσѕρєя K. In addition to that answer, After creation of the Fragments inside an Activity, we can also pass data to the fragments calling methods in Fragments.

For example:

    ArticleFragment articleFrag = (ArticleFragment)
                    getSupportFragmentManager().findFragmentById(R.id.article_fragment);
    articleFrag.updateArticleView(position);
查看更多
梦该遗忘
3楼-- · 2018-12-31 01:29

You can create public static method in fragment where you will get static reference of that fragment and then pass data to that function and set that data to argument in same method and get data via getArgument on oncreate method of fragment, and set that data to local variables.

查看更多
查无此人
4楼-- · 2018-12-31 01:31

the better approach for sending data from activity class to fragment is passing via setter methods. Like

FragmentClass fragmentClass = new FragmentClass();
fragmentClass.setMyList(mylist);
fragmentClass.setMyString(myString);
fragmentClass.setMyMap(myMap);

and get these data from the class easily.

查看更多
若你有天会懂
5楼-- · 2018-12-31 01:33

From Activity you send data with intent as:

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

and in Fragment onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("edttext");    
    return inflater.inflate(R.layout.fragment, container, false);
}
查看更多
情到深处是孤独
6楼-- · 2018-12-31 01:33

This answer may be too late. but it will be useful for future readers.

I have some criteria. I have coded for pick the file from intent. and selected file to be passed to particular fragment for further process. i have many fragments having the functionality of File picking. at the time , every time checking the condition and get the fragment and pass the value is quite disgusting. so , i have decided to pass the value using interface.

Step 1: Create the interface on Main Activity.

   public interface SelectedBundle {
    void onBundleSelect(Bundle bundle);
   }

Step 2: create the Method in the Same Activity

   public void setOnBundleSelected(SelectedBundle selectedBundle) {
       this.selectedBundle = selectedBundle;
   }

Step 3: Create the SelectedBundle reference on the Same Activity

   SelectedBundle selectedBundle;

Step 4: Need to initialise the SelectedBundle reference which are all fragment need filepicker functionality.You place this code on your fragment onCreateView(..) method

    ((MainActivity)getActivity()).setOnBundleSelected(new MainActivity.SelectedBundle() {
          @Override
         public void onBundleSelect(Bundle bundle) {
            updateList(bundle);
        }
     });

Step 5: onActivityResult from the MainActivity, pass the values to the fragments using interface.

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent  data) {
       selectedBundle.onBundleSelect(bundle);
  }

Thats all. Implement every fragment you needed on the FragmentClass. You are great. you have done. WOW...

查看更多
人气声优
7楼-- · 2018-12-31 01:35

Check this answer, if you want to pass the data to fragment after it's created https://stackoverflow.com/a/46467866/1594998

查看更多
登录 后发表回答