Assigning Fragments to tabs in the ActionBar with different orientations

前端 未结 6 2201
半阙折子戏
半阙折子戏 2020-12-23 18:23

I have 3 fragments and an activity. I want to enable tabs on the ActionBar and assign a Fragment to each of the 3 tabs. How do I hook that up cor

6条回答
  •  太阳男子
    2020-12-23 19:25

    I had more or less the same problem, but the solutions presented above did not seem to work out in my situation. Eventually I found the following solution:

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            if (mFragment == null) {
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.replace(android.R.id.content, mFragment, mTag); // Use replace iso add
            }
            else {
                ft.attach(mFragment);
            }
        }
    

提交回复
热议问题