call method from fragment to Activity

前端 未结 4 2113
孤城傲影
孤城傲影 2021-01-26 06:53

hello i have activity and i call many fragment based on my application business i need to call method from fragment in activity i searched in the internet but i cannot find the

4条回答
  •  攒了一身酷
    2021-01-26 07:46

    Your solution is pretty simple, find the fragment instance assuming you have already added it, and call your method as follow:

    HomeFragment homeFragment = (HomeFragment)getSupportFragmentManager().findFragmentByTag("myFragmentTag");
    if(homeFragment != null) //check for null
        homeFragment.addUserLineInfoFragment();
    

提交回复
热议问题