Using Tabs inside android.app.Fragment?

329 views Asked by At

How can I create a Fragment with tabs on the top, hosting other Fragments?

I previously used android.support.v4.app.Fragment but switched to android.app.Fragment because of the PreferenceFragment class. My code was:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_tabs,container, false);


        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
                FragmentB.class, null);

        return rootView;
    }

But this don't work anymore because setup require a v4-FragmentManager.

Any suggestion how to solve this problem?

Thanks in advance!

1

There are 1 answers

0
Anton Kovalyov On BEST ANSWER

As @SubinSebastian answered here :

Add the following project as a library project to your application.

https://github.com/kolavar/android-support-v4-preferencefragment

This solution is better than all other solutions. Add this project as a library project to your workspace. You can keep everything including your fragment transaction as it is and when importing the PreferenceFragment do it like the following.

import android.support.v4.preference.PreferenceFragment;

instead of

import android.preference.PreferenceFragment;