Roman Rdgz
Roman Rdgz

Reputation: 13264

Cannot resolve method getParentFragmentManager

I am cleaning a small Android project, and removing the usage of support libraries and the usage of androidx instead seemed promising. Nevertheless, I am facing an strange problem in my MainActivity.java:

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

private void setupFragments(){
    //...
    ViewPager viewPager = findViewById(R.id.pager);
    ViewPagerAdapter adapter = new ViewPagerAdapter(getParentFragmentManager());
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(2);
    tabLayout.setupWithViewPager(viewPager);
    //...

In the old version, getFragmentManager() was used instead. Nevertheless, now I get the Cannot resolve method getParentFragmentManager compilation error, and get suggested by Android Studio about creating my own getParentFragmentManager function. According to my research, this function should be accessible when importing the androidx libraries, as stated above.

What am I missing here?

EDIT: In case it is related, I am also getting a deprecated warning for FragmentManager from androidx in the definition of my FragmentPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter { ViewPagerAdapter(FragmentManager fm) { // <---- Deprecated super(fm); }

Regarding the version of androidx.fragment:fragment being used, I am only using implementation 'androidx.fragment:fragment-ktx:1.2.2' as suggested in answers (had nothing before that).

Importing androidx.fragment.app.FragmentTransaction gave no changes. Also is shown as not being used.

Upvotes: 1

Views: 5311

Answers (3)

Elio Lako
Elio Lako

Reputation: 1351

You should try using : activity.getSupportFragmentManager()

Upvotes: 3

samlo
samlo

Reputation: 113

The function has been added in the 1.2.0 version of the Fragment component.

Try changing in your build.gradle file (with current stable version):
implementation 'androidx.fragment:fragment-ktx:1.2.2'

Upvotes: 3

NIKHIL AGGARWAL
NIKHIL AGGARWAL

Reputation: 478

Use getSupportFragmentManager(); instead of using getParentFramentManager(); and it should work fine.

Upvotes: -1

Related Questions