92

FragmentManager is deprecated. Is there an alternative or what can I do now?

 PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                destination = place.getName().toString();
                destinationLatLng = place.getLatLng();
            }
            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
            }
        });

deprecation strike image

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Zubair Rajput
  • 921
  • 1
  • 6
  • 3
  • 3
    Welcome to Stackoverflow. Please have a look here for how to ask: https://stackoverflow.com/help/how-to-ask – Vikasdeep Singh Aug 08 '18 at 02:11
  • 2
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Aug 08 '18 at 09:57
  • 1
    @VicJordan: please don't add "home-made tags" into titles; we discourage that here, since we have a proper tagging system. Natural and grammatical sentences are better, either in statement or question form. Here is [the canonical discussion](https://meta.stackoverflow.com/questions/253028/why-is-removing-tags-from-the-title-suggested-so-often). (Please follow this advice for your own question titles too, thanks!). – halfer Aug 08 '18 at 09:58
  • Just use: `getParentFragmentManager()` – TexD Feb 07 '23 at 13:43

14 Answers14

54

This method was deprecated in API level 28.

Use FragmentActivity.getSupportFragmentManager()

faskN
  • 704
  • 5
  • 4
  • 10
    I think this answer is outdated - it is simply `getSupportFragmentManager()` now – kc_dev Aug 15 '21 at 21:41
  • 1
    @kc_dev There is no getSupportFragmentManager() in Fragment of AndroidX. The deprecation message says you have to use getParentFragmentManager(). Fragmentmanagers are pretty much the biggest crap in Android since fragments were introduced. :) – The incredible Jan Jan 26 '23 at 10:23
51

If I understood you well getFragmentManager() is now deprecated

we will use getChildFragmentManager() Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

we will use getParentFragmentManager() Return the FragmentManager for interacting with fragments associated with this fragment's activity.

so, if you deal with fragments inside a fragment you will use the first one and if you deal with fragments inside an activity you will use the second one.

you can find them here package androidx.fragment.app;

Mina Samir
  • 1,527
  • 14
  • 15
  • https://developer.android.com/reference/androidx/fragment/app/Fragment#getFragmentManager() – Hafez Divandari Jul 29 '20 at 02:38
  • Refer here for including the package: https://developer.android.com/jetpack/androidx/releases/fragment#declaring_dependencies – mr5 Oct 16 '20 at 14:35
  • +1. I tested both `getChildFragmentManager()` and `getParentFragmentManager()` for a fragment popup inside of another fragment (i.e. Bottom Sheet Fragment inside of another fragment), but both are working without any error! Do you the reason of that? – C.F.G Feb 04 '23 at 14:59
21

If you using AndroidX Fragment androidx.fragment.app.Fragment you can use

getActivity().getSupportFragmentManager()
teteArg
  • 3,684
  • 2
  • 20
  • 18
14

Right now, in Kotlin I use

val fragmentManager = this@className.parentFragmentManager

or (prefer this one)

val fragmentManager = this@className.childFragmentManager

well if you still use Java I think the code is

FragmentManager manager = this.getParentFragmentManager();

Hope my answer can solve the task.

I recommended you to use Navigation now

Liong
  • 1,324
  • 12
  • 18
12

I used parentFragmentManager instead of fragmentManager. (If you are using androidx Fragment.)

https://developer.android.com/jetpack/androidx/releases/fragment#1.2.0

getFragmentManager() deprecation: The getFragmentManager() and requireFragmentManager() methods on Fragment have been deprecated and replaced with a single getParentFragmentManager() method, which returns the non-null FragmentManager the Fragment is added to (you can use isAdded() to determine if it is safe to call).

pringi
  • 3,987
  • 5
  • 35
  • 45
Yoju
  • 179
  • 2
  • 4
9

Since the question is a little broad, here's the solution for those who have migrated to androidX.

Make sure that your fragments are using androidX versions in import section. i.e if you're using DialogFragment(), replace

import android.app.DialogFragment

with

import androidx.fragment.app.DialogFragment

Then for fragmentManager when show()ing the dialog, if launching from another fragment don't change it, but if passing from an Activity, first extend the activity from FragmentActivity(), then pass supportFragmentManager:

// kotlin
import androidx.fragment.app.FragmentActivity

class MyActivity: FragmentActivity() {
    ...
    val myDialog = MyDialog()
    myDialog.show(supportFragmentManager, TAG)
    ...
}

Although, the same steps also apply to support.v4 if you've not migrated to androidX. Just in the first step replace

import android.app.DialogFragment

with

import android.support.v4.app.Fragment
SIMMORSAL
  • 1,402
  • 1
  • 16
  • 32
8

You should use the Fragment class from Android Support library instead.

android.app.Fragment was deprecated in API level 28. Use the Support Library Fragment for consistent behavior across all devices and access to Lifecycle.

Use android.support.v4.app.Fragment instead.

midhunhk
  • 5,560
  • 7
  • 52
  • 83
5

for Kotlin:

activity
    ?.supportFragmentManager
    ?. <whatever needs to come after that>
AndroidDev
  • 20,466
  • 42
  • 148
  • 239
4

Use Support Library overcome this issue.

As per documentation Fragment class has been deprecated:

This class was deprecated in API level 28. Use the Support Library Fragment for consistent behavior across all devices and access to Lifecycle.

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
4

Use This

import android.support.v4.app.FragmentManager;

Instead of

import android.app.FragmentManager;

And Use This

FragmentManager manager = ((YourActivity) mContext).getSupportFragmentManager();

Instead of

 FragmentManager manager = ((YourActivity) mContext).getFragmentManager();
yadav_nkit
  • 41
  • 3
0

Xml

<fragment
    android:id="@+id/place_autocomplete_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment"
/>

Activity

SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment)
            getSupportFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
Karthikeyan
  • 196
  • 2
  • 10
A.Klapchuk
  • 27
  • 3
  • from what class do you call this `getSupportFragmentManager()` ? Please give a bit more of a detailed example – CybeX May 25 '19 at 10:02
0

If you're displaying a DialogFragment over a PreferenceFragment (specifically an android.preference.PreferenceFragment not the class from the Support Preference library), then you will see this warning.

Approximately June 2018, Google deprecated the PreferenceFragment and all its associated methods like getFragmentManager(). You probably already know that getSupportFragmentManager() cannot be used on the PreferenceFragment anyway, as it's a special exception.

The recommended approach now is to use the PreferenceFragmentCompat from the AndroidX Jetpack library or from the Preference Support library:

implementation "androidx.preference:preference:1.0.0"

You can see available versions here: https://maven.google.com -> Search for "preference"

More info here: Android- deprecated method warning regarding PreferenceActivity


Be aware that migrating to PreferenceFragmentCompat may be a lengthy and difficult task, especially if you have custom Preference classes. More info:

Mr-IDE
  • 7,051
  • 1
  • 53
  • 59
0

Replace getFragmentManager() with parentFragmentManager .

Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36
-2

Extend AppCompatActivity which by default extends FragmentActivity, then you can use getSupportFragmentManager() which will return you the fragmentManager.

androidStud
  • 532
  • 4
  • 9