Send data from activity to fragment in Android

前端 未结 20 2716
悲哀的现实
悲哀的现实 2020-11-21 05:13

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 doInBa

20条回答
  •  清歌不尽
    2020-11-21 05:19

    From Activity you send data with Bundle as:

    Bundle bundle = new Bundle();
    bundle.putString("data", "Data you want to send");
    
    // Your fragment
    MyFragment obj = new MyFragment();
    obj.setArguments(bundle);
    

    And in Fragment onCreateView method get the data:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        String data = getArguments().getString("data");// data which sent from activity  
        return inflater.inflate(R.layout.myfragment, container, false);
    }
    

提交回复
热议问题