TransWikia.com

FragmentTransaction behaving differently for different android sdk releases

Stack Overflow Asked by jack5432109 on December 27, 2021

I am working on an android project with this in build.gradle

android {
    compileSdkVersion 25
}
dependencies {
    implementation 'com.android.support:support-v13:25.+'
}

I have a class called HistoryFragment.java in it

public class HistoryFragment extends Fragment {

    private RecentHistoryPagerAdapter historyPagerAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.view_pager, container, false);
        if (savedInstanceState == null) {
            historyPagerAdapter = new HistoryPagerAdapter(getChildFragmentManager());
            viewPager = (ViewPager) rootView.findViewById(R.id.view_pager);
            viewPager.setAdapter(historyPagerAdapter);
            setTabListeners();
        }
        return rootView;
    }

    public void viewSummary () {
        HistoryFragment historyFragment = (HistoryFragment) getActivity().getFragmentManager().findFragmentById(R.id.container);
        FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();
        transaction.hide(historyFragment);
        
        SummaryFragment summaryFragment = SearchResultSummaryFragment.newInstance();
        transaction.add(R.id.container, summaryFragment, "summaryFragment");
        transaction.commit();
        return;

here,

HistoryPagerAdapter extends android.support.v13.app.FragmentPagerAdapter

It works correctly and results in this.

expected behavior

I copied this to another project with this in its build.gradle

android {
    compileSdkVersion 27
}
dependencies {
    implementation 'com.android.support:support-v13:27.1.1'
}

It behaves incorrectly. While SummaryFragment does show up, it retains historyPagerAdapter as we can see in this next image

unexpected behavior

One Answer

As already suggested in the comments, I would also recommend to switch your project to AndroidX libraries. This can be easily done by right-clicking on your module (In the Project pane), then select Refactor... and then Migrate to AndroidX...

Android Studio will then replace your gradle dependencies with the new androidx dependencies and change all import statements accordingly. I have done this also with big multi-module projects and it worked almost perfectly everytime. Maybe you have to adjust an import here and there.

Specific to your question, the new grade dependency for fragments is implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0' (without the "-ktx" if you are using Java).

You should not and you cannot mix android.support with anroidx.. It's either the one or the other. Both together do not work!

You can find an overview of all new dependencies here: https://developer.android.com/jetpack/androidx/migrate/class-mappings

Answered by muetzenflo on December 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP