【问题标题】:back stack and statically added fragment回栈和静态添加的片段
【发布时间】:2014-01-13 16:46:22
【问题描述】:

我有一个从 XML 静态添加的片段我想用另一个片段替换这个片段,我通过添加以下代码来做到这一点:

        CFragment singleStationFragment = new CFragment();
        android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.layoutlist, singleStationFragment);
        transaction.addToBackStack(null);
        transaction.commit();

问题是,当我按下后退按钮时,第一个片段没有显示,因为它不是通过事务添加的,经理也不知道,有没有办法可以添加第一个片段(已经添加FROM XML),到我的后台堆栈,或者我可以在单击返回而不是退出应用程序时显示它?谢谢!

【问题讨论】:

    标签: android android-fragments back-button back-stack fragmenttransaction


    【解决方案1】:

    据我所知,您必须在代码中而不是在布局文件中将您的第一个片段添加到活动中。使用FragmentTransactionadd 方法来做到这一点

    transaction.add(R.id.FragmentContainer, fragment);
    

    【讨论】:

    • 抱歉没用,我会尝试其他解决方案并更新我的帖子。感谢和抱歉迟到的回复
    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多