Send data from activity to fragment in Android

前端 未结 20 2702
悲哀的现实
悲哀的现实 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:13

    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);
    }
    

提交回复
热议问题