FragmentManager, LocalActivityManager and TabHost.

2019-07-13 04:46发布

问题:

Ok, I have a class that inherits from FragmentManager. Inside this class I have a FragmentPagerAdapter that create some fragment tabs and insert in my TabHost. The code compiles fine and works. I put some parts of theses classes below. I'm using to do this ths android supprt package v4

public class TabMenu extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener{
...
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs_container);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    tabHost = (TabHost)findViewById(android.R.id.tabhost);
    tabHost.setup();

    viewPager = (ViewPager)findViewById(R.id.viewpager);

    ...

    menuAdapter = new MenuPagerAdapter(this, tabHost, viewPager);
    menuAdapter.addTab("meutime", "Meu Time", MeuTime.class, extras);
    ...
    tabHost.setOnTabChangedListener(this);
    viewPager.setOnPageChangeListener(this);
            ...

public class MenuPagerAdapter extends FragmentPagerAdapter{
    public MenuPagerAdapter(FragmentActivity fragmentActivity, TabHost tabHost, ViewPager viewPager) {
        ...
    }

    @Override
    public Fragment getItem(int position) {
        String currentFragment = tabNames.get(position);
        return fragmentManager.findFragmentByTag(currentFragment);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return tabNames.size();
    }

    public void addTab(String tag, String tabLabel, Class<?> cls, Bundle bundle){
        TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag).setIndicator(createTab(tabLabel));
        Intent intent = new Intent();
        intent.setClass(context, cls);
        intent.putExtras(bundle);
        tabSpec.setContent(intent);
        tabNames.add(tag);
        tabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

    private View createTab(String tabLabel){
        View view = LayoutInflater.from(context).inflate(R.layout.tab_spec_layout, null, false);
        ...
        return view;
    }
}

When I instantiate this FragmentActivity I received the following exception:

java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?

Reading on Android developer' site the LocalActivityManager is deprecated. But it is used in TabHost.setup(LocalActivityManager). If this class is deprecated, is there a solution for this case? I can't use TabContentFactory.

回答1:

Unhapilly I've used TabContentFactory. Personally I did't like this solution to create a empty view but works.

public void addTab(String tag, final String tabLabel, Class<?> cls, Bundle bundle){
        TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag).setIndicator(createTab(tabLabel));
        tabSpec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                View view = new View(context);
                view.setMinimumHeight(0);
                view.setMinimumWidth(0);
                return view;
            }
        });

        fragContentInstances.add(new FragContentInfos(cls, bundle));
        tabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

    private class FragContentInfos{
        private Class<?> cls;
        private Bundle bundle;
        public FragContentInfos(Class<?> cls, Bundle bundle){
            this.cls = cls;
            this.bundle = bundle;
        }
    }