1

Fragments在从FragmnetActivity. 我可以添加初始片段,然后根据我的应用程序设置,我正在替换片段。当我处于调试模式时,我观察到这一点,onCreate并且非常新的 Fragmnet 的其他overriden方法被调用了两次。为什么这样?以及如何限制它们只被调用一次。

以下是我在应用程序中使用 Fragments 的方式:

public class FragmentTabs extends FragmentActivity implements OnTabChangeListener {

private FragmentTabHost mTabHost;
private HomeFragment homeFragmnet;
private GroupFragment groupFragment;
private TemplateFragment templateFragment;
private SetingsFragment settingsFragment;
private CalendarViewFragment calendarViewFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.fragment_tabs);

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    homeFragmnet = new HomeFragment();
    groupFragment = new GroupFragment();
    templateFragment = new TemplateFragment();
    settingsFragment = new SetingsFragment();
    calendarViewFragment = new CalendarViewFragment();

    mTabHost.setOnTabChangedListener(this);

    if (Database.getSharedObject(getApplicationContext()).getAppSettings().getListView() == 1) {
        mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("", getResources().getDrawable(R.drawable.home_tab)), HomeFragment.class, null);

    } else {
        mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("", getResources().getDrawable(R.drawable.home_tab)), CalendarViewFragment.class, null);
    }
    mTabHost.addTab(mTabHost.newTabSpec("groups").setIndicator("", getResources().getDrawable(R.drawable.groups_tab)), GroupFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("templates").setIndicator("", getResources().getDrawable(R.drawable.templates_tab)),TemplateFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("settings").setIndicator("", getResources().getDrawable(R.drawable.settings_tab)), SetingsFragment.class, null);

}

@Override
protected void onResume() {
    super.onResume();

}

private void showHomeFragment() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.realtabcontent, homeFragmnet);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.disallowAddToBackStack();
    ft.commit();
}

private void showGroupsFragment() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.realtabcontent, groupFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.disallowAddToBackStack();
    ft.commit();
}

private void showTemplateFragment() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.realtabcontent, templateFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.disallowAddToBackStack();
    ft.commit();
}

private void showSettingsFragment() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.realtabcontent, settingsFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.disallowAddToBackStack();
    ft.commit();
}

private void showCalendarViewFragment() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.realtabcontent, calendarViewFragment);
    ft.disallowAddToBackStack();
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

@Override
public void onTabChanged(String arg0) {

    if (mTabHost.getCurrentTab() == 0) {
        if (Database.getSharedObject(getApplicationContext()).getAppSettings().getListView() == 1) {
            showHomeFragment();
        } else {
            showCalendarViewFragment();
        }
    } else if (mTabHost.getCurrentTab() == 1) {
        showGroupsFragment();
    } else if (mTabHost.getCurrentTab() == 2) {
        showTemplateFragment();
    } else if (mTabHost.getCurrentTab() == 3) {
        showSettingsFragment();
    }
}

任何帮助表示赞赏。谢谢!

4

1 回答 1

0

我改用 show() 和 hide(),效果很好。

private void showHomeFragment() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    if (homeFragmnet.isAdded())
        ft.show(homeFragmnet);
    else
        ft.add(R.id.realtabcontent, homeFragmnet);

    if (groupFragment.isAdded()) ft.hide(groupFragment);
    if (templateFragment.isAdded()) ft.hide(templateFragment);
    if (settingsFragment.isAdded()) ft.hide(settingsFragment);
    if (calendarViewFragment.isAdded()) ft.hide(calendarViewFragment);

    ft.commit();
}
于 2013-03-21T10:44:49.273 回答