Can I programmatically convert between R.id ids and class of a fragment?

Viewed 25

Suppose I have a Fragment called Fragment MenuFragment and it have R.id android:id="@+id/MenuFragment" inside nav_graph.xml

Question 1: Can I programmatically find R.id if I have just MenuFragment.class ?

Question 2: Can I programmatically get MenuFragment.class if I have just R.id.MenuFragment ?

1 Answers
  1. No. A fragment can have infinitely many ids. If you have an instance of a fragment you can get its id, but you could have the same fragment in a dozen activities with a dozen different ids. Heck, you can have a dozen copies in the same activity with a dozen ids.

  2. This, yes. If you use the id to get the actual instance of the fragment, you can then use .getClass() to get its class.

Related