Since the Android getFragmentManager() API is deprecated, is there any alternative?

前端 未结 12 1508
梦毁少年i
梦毁少年i 2020-12-05 06:47

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

 PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocomplet         


        
相关标签:
12条回答
  • 2020-12-05 07:03

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

    0 讨论(0)
  • 2020-12-05 07:06

    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);
    
    0 讨论(0)
  • 2020-12-05 07:09

    This method was deprecated in API level 28.

    Use FragmentActivity.getSupportFragmentManager()

    0 讨论(0)
  • 2020-12-05 07:10

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

    getActivity().getSupportFragmentManager()
    
    0 讨论(0)
  • 2020-12-05 07:12

    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();
    
    0 讨论(0)
  • 2020-12-05 07:20

    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.

    0 讨论(0)
提交回复
热议问题