How to send data from Activity to Fragment

前端 未结 5 1863
盖世英雄少女心
盖世英雄少女心 2020-12-05 21:44

I know there are many topics about this here. I have also read documentation many times but I can\'t find the best way to pass data from activity to fragment.

I want

5条回答
  •  余生分开走
    2020-12-05 21:56

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

    see detail answer here..

提交回复
热议问题