1

I am trying to refresh a fragment with the code given in this answer:

// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();

However, I don't know what is the tag of my fragment, which is a Navigation Drawer Fragment.

As "Your_Fragment_Tag" I have tried to use the id of content_main.xml, "nav_host_fragment_content_main":

<fragment
    android:id="@+id/nav_host_fragment_content_main"
    android:name="androidx.navigation.fragment.NavHostFragment"
    .... />

And the id of the fragment in mobile_navigation.xml, "nav_settings":

<fragment
    android:id="@+id/nav_settings"
    android:name="com.geology_quiz_and_guide.mineralogy.ui.settings.SettingsFragment"
    android:label="@string/menu_settings"
    tools:layout="@layout/fragment_settings" />

Both produce null object reference error in the line of the detach ft.detach(frg);

How can I access to my fragment dynamically on my Navigation Drawer Project?

  • As it is inside the Fragment class I can use `Fragment frg = this;` –  Dec 26 '22 at 18:34

0 Answers0