[android] 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?

This question is related to android android-activity android-fragments

The answer is


Instead of

extends Fragment

use

extends android.support.v4.app.Fragment 

This works for me. for *API14 and above


get current activity from parent, then using this code

getActivity().getSupportFragmentManager()

import

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

I used FragmentActivity

TabAdapter = new TabPagerAdapter(((FragmentActivity) getActivity()).getSupportFragmentManager());

This worked for me. Running android API 19 and above.

FragmentManager fragMan = getFragmentManager();

getCurrentActivity().getFragmentManager()

Simply Use

 FragmentManager fm = getActivity().getSupportFragmentManager();

Remember always when accessing fragment inflating in MainLayout use Casting or getActivity().


You need to extend FragmentActivity instead of Activity


extend class to AppCompatActivity instead of Activity


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-activity

Kotlin Android start new Activity The activity must be exported or contain an intent-filter How to define dimens.xml for every different screen size in android? Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which? Not an enclosing class error Android Studio java.lang.IllegalStateException: Fragment not attached to Activity Soft keyboard open and close listener in an activity in Android android.app.Application cannot be cast to android.app.Activity Android Shared preferences for creating one time activity (example) Android ListView with onClick items

Examples related to android-fragments

FragmentActivity to Fragment How to start Fragment from an Activity How to use data-binding with Fragment In android how to set navigation drawer header image and name programmatically in class file? Android Fragment onAttach() deprecated How to convert any Object to String? Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which? Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments java.lang.IllegalStateException: Fragment not attached to Activity java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference