35

I'm trying to setup in my app that is using androidX.

My problem is that when I try to work with PlaceAutocompleteFragment I get errors because it is a fragment from android.app.fragment and my parent fragment is an androidx fragment: androidx.fragment.app.Fragment so it uses a androidx.fragment.app.FragmentManagerinstead of a android.app.FragmentManager.

How can I work with "old" fragments in androidX?

Addev
  • 31,819
  • 51
  • 183
  • 302

3 Answers3

39
  1. Add this to dependencies: implementation 'androidx.appcompat:appcompat:1.0.2'.
  2. Add this: import androidx.fragment.app.FragmentTransaction;.
  3. Switch your activity extends from Activity to AppCompatActivity.
  4. Change from getFragmentManager() to getSupportFragmentManager().

This will fix your issue.

leonheess
  • 16,068
  • 14
  • 77
  • 112
viswanath
  • 401
  • 4
  • 7
12

If you are using the new libraries then only use those, don't combine them bacause yo can run into more problems. Now for your problem go to your fragment and just change the import form:

import android.app.fragment

To:

import androidx.fragment.app.Fragment

That should solve your problem.

Diego Lovera
  • 166
  • 5
  • This does not solve everything. It appears that getFragmentManager() has not been moved to the androidX class. So there is no real simple replacement for that. Does anybody know how to replace getFragmentManager()? – Rainer Glüge Oct 13 '18 at 00:13
  • 17
    OK so activity must extend `FragmentActivity` from `androidx.fragment.app.FragmentActivity`, then one can use `getSupportFragmentManager()` instead of `getFragmentManager()`; – Rainer Glüge Oct 13 '18 at 00:24
  • That´s rigth, `getFragmentManager()` has been replaced by `getSupportFragmentManagaer()` since the introduction of the support libraries. _Base class for activities that want to use the support-based Fragment and Loader APIs. When using this class as opposed to new platform's built-in fragment and loader support, you must use the getSupportFragmentManager() and getSupportLoaderManager() methods respectively to access those features._ [Source](https://developer.android.com/reference/android/support/v4/app/FragmentActivity) @RainerGlüge – Diego Lovera Oct 15 '18 at 02:25
4

There is not any need to get the FragmentManager directly from the Activity because a replacement method has been provided in the Fragment called getParentFragmentManager:

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

From the docs for the deprecated getFragmentManager:

 * @deprecated This has been removed in favor of <code>getParentFragmentManager()</code> which
 * throws an {@link IllegalStateException} if the FragmentManager is null. Check if
 * {@link #isAdded()} returns <code>false</code> to determine if the FragmentManager is
 * <code>null</code>.

The docs for the replacement method getParentFragmentManager:

 * Return the FragmentManager for interacting with fragments associated
 * with this fragment's activity.  Note that this will available slightly
 * before {@link #getActivity()}, during the time from when the fragment is
 * placed in a {@link FragmentTransaction} until it is committed and
 * attached to its activity.
 *
 * <p>If this Fragment is a child of another Fragment, the FragmentManager
 * returned here will be the parent's {@link #getChildFragmentManager()}.
 *
 * @throws IllegalStateException if not associated with a transaction or host.

Just take care to check isAdded to make sure that the FragmentManager is not null.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
  • perfectly worked for me i replaced getFragmentManager to getParentFragmentManager and it worked thanks man – Vikas Kandari Mar 13 '20 at 08:53
  • didn't work for me, it says "Cannot resolve method 'getParentFragmentManager()'" – temirbek Mar 20 '20 at 16:08
  • It has to be done using: "import androidx.fragment.app.Fragment". [Docs](https://developer.android.com/reference/androidx/fragment/app/Fragment). It could happen that the app has not been upgraded to the androidx support libraries? – Elletlar Mar 20 '20 at 23:04
  • Did you try this? The question below suggests that it won't work https://stackoverflow.com/questions/60848166/cannot-resolve-method-getparentfragmentmanager – Paul McCarthy Jan 11 '23 at 13:22