Send data from activity to fragment in Android

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

    My solution is to write a static method inside the fragment:

    public TheFragment setData(TheData data) {
        TheFragment tf = new TheFragment();
        tf.data = data;
        return tf;
    }
    

    This way I am sure that all the data I need is inside the Fragment before any other possible operation which could need to work with it. Also it looks cleaner in my opinion.

提交回复
热议问题