Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot call getSupportFragmentManager() from activity

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?

like image 732
Taru Avatar asked Nov 18 '12 19:11

Taru


People also ask

How do I get fragment manager in fragment?

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() .

What is the use of getSupportFragmentManager?

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 .

What is FragmentManager in Android?

A FragmentManager manages Fragments in Android, specifically it handles transactions between fragments. A transaction is a way to add, replace, or remove fragments.


1 Answers

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.

like image 67
Robert Estivill Avatar answered Sep 28 '22 14:09

Robert Estivill