Unable to call getSupportFragmentManager () from action

I have an activity that 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 the method from ResultFragment from the method in Activity, but getSupportFragmentManager "does not exist":

 FragmentManager fragMan = getSupportFragmentManager(); 

How can i solve this?

+70
android android-activity android-fragments
Nov 18 '12 at 19:37
source share
10 answers

Your activity does not extend FragmentActivity from the support library, so this method is not in the superclass

If you are targeting api 11 or higher, you can use Activity.getFragmentManager instead.

+173
Nov 18 '12 at 19:40
source share

extend class to AppCompatActivity instead of Activity

+40
Sep 28 '15 at 18:03
source share

get current activity from parent and then use this code

 getActivity().getSupportFragmentManager() 
+14
Aug 04 '15 at 20:17
source share

import

 import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; 
+4
Oct 02 '14 at 1:46
source share

Just use

  FragmentManager fm = getActivity().getSupportFragmentManager(); 

Remember that always when accessing a fragment break in MainLayout Casting or getActivity() .

+1
Nov 12 '16 at 7:13
source share

FragementAvtivity extension instead of activity

+1
Feb 07 '18 at 7:03
source share

It worked for me. Running the Android API 19 and above.

 FragmentManager fragMan = getFragmentManager(); 
+1
Apr 10 '18 at 7:51
source share
 getCurrentActivity().getFragmentManager() 
0
Nov 12 '16 at 1:11
source share

If you write code which dose does not exist in the context of the fragment (Activity), suppose the adapter, then you can convert your Context to the adapter in ActivityFragment, for example:

FragmentActivity activity = (FragmentActivity) mContext; FragmentManager manager = activity.getSupportFragmentManager();

-one
Jul 20 '17 at 10:55
source share

I used FragmentActivity TabAdapter = new TabPagerAdapter (((FragmentActivity) getActivity ()). GetSupportFragmentManager ());

-2
Aug 30 '15 at 11:11
source share



All Articles