从另一个恢复的片段不显示

时间:2015-05-10 16:41:42

标签: android android-fragments

我有一个应用,其中MainActivity有一个底部tabBar和一个fragmentContainer(FrameLayout),当用户点击Bottb栏中的按钮时,我使用FragmentManager进行替换当前显示的片段。在其中一个片段中,我有另一个tabBar(在顶部)和其他fragmentContainer(与另一个id)。在系统开发选项中,我检查了立即销毁活动的选项。我正在使用onSaveInstanceStategetFragmentManager().putFragment()来保存片段状态,当前标签和用户输入数据。当用户按下主页按钮然后再次打开应用程序时,主片段恢复没有问题,但什么时候应该恢复de inner片段,它不显示,只是不显示,但是正确添加甚至用户输入数据。在恢复状态之前,我对替换片段没有任何问题。我需要一些帮助:(

更新

我的代码看起来像这样:

public class MainActivity extends FragmentActivity{

int tabId = 0;
final View.OnClickListener tabController = new View.OnClickListener() {
    @Override
    public void onClick(View tab) {
        if (tab.getId() == tabId) return;
        tabId = tab.getId();
        if (tabId == R.id.tab1) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, new Fragment1()).commit();
        } else if (tabId == R.id.tab2) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, new FragmentWhitTopBar()).commit();
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    /*  SetingUp activity views */

    tab1.setOnClickListener(tabController);
    tab2.setOnClickListener(tabController);

    if (savedInstanceState != null) {
        this.tabId = savedInstanceState.getInt(CURRENT_TAB);
        Fragment fragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_TAB);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
        /* All OK Yay!!! */
    } else {
        tabController.onClick(tabTransfer);
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putInt(CURRENT_TAB, tabId);
    if (shouldSaveInstanceState) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainer);
        if (fragment != null)
            getSupportFragmentManager().putFragment(outState, FRAGMENT_TAB, fragment);
    }
    super.onSaveInstanceState(outState);
}
}

public class FragmentWhitTopBar extends Fragment{

int tabId = 0;
final View.OnClickListener tabController = new View.OnClickListener() {
    @Override
    public void onClick(View tab) {
        if (tab.getId() == tabId) return;
        tabId = tab.getId();
        if (tabId == R.id.topTab1) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, new Fragment2()).commit();
        } else if (tabId == R.id.topTab2) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, new Fragment3()).commit();
        }
    }
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_with_top_bar, container, false);

    /*  SetingUp fragment views */

    topTab1.setOnClickListener(tabController);
    topTab2.setOnClickListener(tabController);

    if (savedInstanceState != null) {
        this.tabId = savedInstanceState.getInt(CURRENT_TOP_TAB);
        Fragment fragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_TOP_TAB);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
        /* Working but Not Showing D: !!! */
    } else {
        tabController.onClick(tabTransfer);
    }
    return root;
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putInt(CURRENT_TOP_TAB, tabId);
    if (shouldSaveInstanceState) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainer);
        if (fragment != null)
            getSupportFragmentManager().putFragment(outState, FRAGMENT_TOP_TAB, fragment);
    }
    super.onSaveInstanceState(outState);
}
}

1 个答案:

答案 0 :(得分:1)

如果您在Fragment内使用Fragment,那么对于嵌套片段,请使用getChildFragmentManager()