Since the Android getFragmentManager() API is deprecated, is there any alternative?
JavaAndroidJava Problem Overview
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.
}
});
Java Solutions
Solution 1 - Java
This method was deprecated in API level 28.
Use FragmentActivity.getSupportFragmentManager()
Solution 2 - Java
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;
Solution 3 - Java
If you using AndroidX Fragment androidx.fragment.app.Fragment
you can use
getActivity().getSupportFragmentManager()
Solution 4 - Java
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
Solution 5 - Java
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).
Solution 6 - Java
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.
Solution 7 - Java
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
Solution 8 - Java
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.
Solution 9 - Java
for Kotlin:
activity
?.supportFragmentManager
?. <whatever needs to come after that>
Solution 10 - Java
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();
Solution 11 - Java
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: https://stackoverflow.com/questions/14756457/android-deprecated-method-warning-regarding-preferenceactivity#51149808
Be aware that migrating to PreferenceFragmentCompat may be a lengthy and difficult task, especially if you have custom Preference classes. More info:
- https://stackoverflow.com/questions/32621403/how-do-i-create-custom-preferences-using-android-support-v7-preference-library
- https://github.com/jaredrummler/ColorPicker/issues/26#issuecomment-331626888
- https://issuetracker.google.com/issues/119256512
- https://issuetracker.google.com/issues/124153118
- https://stackoverflow.com/questions/18509369/android-how-to-remove-margin-padding-in-preference-screen
Solution 12 - Java
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);
Solution 13 - Java
Replace getFragmentManager()
with parentFragmentManager
.
Solution 14 - Java
Extend AppCompatActivity which by default extends FragmentActivity, then you can use getSupportFragmentManager() which will return you the fragmentManager.