Send data from activity to fragment in Android

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

    If you pass a reference to the (concrete subclass of) fragment into the async task, you can then access the fragment directly.

    Some ways of passing the fragment reference into the async task:

    • If your async task is a fully fledged class (class FooTask extends AsyncTask), then pass your fragment into the constructor.
    • If your async task is an inner class, just declare a final Fragment variable in the scope the async task is defined, or as a field of the outer class. You'll be able to access that from the inner class.

提交回复
热议问题