I have an activity which has a fragment.
XML:
<fragment android:name="com.example.androidcalculator.ResultFragment" android:id="@+id/result_fragment" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" />
And I want to call a method from ResultFragment from a method in the Activity, but getSupportFragmentManager "doesn't exist":
FragmentManager fragMan = getSupportFragmentManager();
How can I resolve this?
Accessing in a Fragment Inside a fragment, you can get a reference to the FragmentManager that manages the fragment's children through getChildFragmentManager() . If you need to access its host FragmentManager , you can use getParentFragmentManager() .
getSupportFragmentManager and getChildFragmentManager FragmentManager is class provided by the framework which is used to create transactions for adding, removing or replacing fragments. getSupportFragmentManager is associated with Activity consider it as a FragmentManager for your Activity .
A FragmentManager manages Fragments in Android, specifically it handles transactions between fragments. A transaction is a way to add, replace, or remove fragments.
Your activity doesn't extend FragmentActivity from the support library, therefore the method is not present in the superclass
If you are targeting api 11 or above, you could use Activity.getFragmentManager instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With