Send data from activity to fragment in Android

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

    Basic Idea of using Fragments (F) is to create reusable self sustaining UI components in android applications. These Fragments are contained in activities and there are common(best) way of creating communication path ways from A -> F and F-A, It is a must to Communicate between F-F through a Activity because then only the Fragments become decoupled and self sustaining.

    So passing data from A -> F is going to be the same as explained by ρяσѕρєя K. In addition to that answer, After creation of the Fragments inside an Activity, we can also pass data to the fragments calling methods in Fragments.

    For example:

        ArticleFragment articleFrag = (ArticleFragment)
                        getSupportFragmentManager().findFragmentById(R.id.article_fragment);
        articleFrag.updateArticleView(position);
    

提交回复
热议问题