![]() ![]() However, this does not specify the ID for a Fragment. There is however, a String tag that you can set inside the FragmentTransaction which can be used to uniquely identify a Fragment.Īs Aleksey pointed out, you can pass an ID to FragmentTransaction’s add(int, Fragment)method. You can’t set a fragment’s ID programmatically. Then you can use the command FragmentManager.findFragmentByTag(String tab) to find the fragment in question. You can set the tag value in FragmentTransaction.add(Fragment fragment, String tag ). In most cases you can use the fragment tag as well as the ID. Will get you whatever (awesome) fragment is held in R.id.fragment_holder. add(R.id.fragment_holder, new AwesomeFragment())Īnd then AwesomeFragment awesome = (AwesomeFragment) The important part is “as the container ID when added in a transaction”. When inflated from XML or as the container ID when added in Turns out you may not need to know the fragment id.įrom the docs: public abstract Fragment findFragmentById (int id)įinds a fragment that was identified by the given id either
0 Comments
Leave a Reply. |