Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentManager and androidx FragmentManager

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?

like image 509
Addev Avatar asked Sep 10 '18 11:09

Addev


People also ask

What is the difference between FragmentManager and Supportfragmentmanager?

FragmentManager is class provided by the framework which is used to create transactions for adding, removing or replacing fragments. getSupportFragmentManager is associated with Activity consider it as a FragmentManager for your Activity .

What is a FragmentManager?

FragmentManager is the class responsible for performing actions on your app's fragments, such as adding, removing, or replacing them, and adding them to the back stack.

Is Fragment Manager deprecated?

This class was deprecated in API level 28. Use the Support Library androidx.


2 Answers

  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.

like image 171
viswanath Avatar answered Oct 08 '22 21:10

viswanath


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.

like image 41
Diego Lovera Avatar answered Oct 08 '22 20:10

Diego Lovera