Send data from activity to fragment in Android

2018-12-31 01:05发布

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 doInBackground I get some result, which I save to variable. How can I send this variable from subclass "my activity" to this fragment?

19条回答
只若初见
2楼-- · 2018-12-31 01:36

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.
查看更多
登录 后发表回答