How to know Fragment id for the fragment(s) provided by the tabbed activity template

孤者浪人 提交于 2021-02-20 10:12:49

问题


I have used the tabbed activity template provided by android studio but i can't happen to find the id for the different fragments used. the template had only one .xml and .java for all three fragments. I made a few changes and made three separate .xml and .java for the three fragments. But I can't figure out how to set the id for the different fragments either from .xml or in .java and without the id I can't perform inter fragment communication.


回答1:


Now for retrieving a fragment

Fragment f = getSupportFragmentManager().findFragmentByTag(getFragmentTag(mViewPager.getId(), mViewPager.getCurrentItem()));


public static String getFragmentTag(int viewId, long id) {
    return "android:switcher:" + viewId + ":" + id;
}


来源:https://stackoverflow.com/questions/39555081/how-to-know-fragment-id-for-the-fragments-provided-by-the-tabbed-activity-temp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!