Android添加片段后退后无法显示

时间:2018-05-28 03:31:20

标签: android fragment fragmenttransaction fragment-backstack

我有3个片段fragmentA,fragmentB,fragmentC默认添加到create上的活动中以保留它之后的实例,我有另一个片段fragmentX,它将被添加到片段容器上,带有backStack on Button click,fragmentX在添加时显示,但是当我需要再次显示它时它不起作用。

public class MainActivity extends ActivityBaseClass {

private Fragment fragmentA;
private Fragment fragmentB;
private Fragment fragmentC;
private Fragment fragmentX;

@BindView(R.id.btn_show_fragmentx)
Button mBtnShowFragmentX;
@BindView(R.id.tab_layout)
TabLayout mTab;

private FragmentTransaction transaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    fragmentA = FragmentA.newInstance();
    fragmentB = FragmentB.newInstance();
    fragmentC = FragmentC.newInstance();

    transaction = getSupportFragmentManager().beginTransaction();
    transaction.add(R.id.fragment_container, fragmentA, "A");
    transaction.add(R.id.fragment_container, fragmentB, "B");
    transaction.add(R.id.fragment_container, fragmentC, "C");
    transaction.commit();


    mTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            transaction = getSupportFragmentManager().beginTransaction();
            switch (tab.getPosition()) {
                case 0:
                    transaction.show(fragmentA);
                    transaction.hide(fragmentB);
                    transaction.hide(fragmentC);
                    break;
                case 1:
                    transaction.show(fragmentB);
                    transaction.hide(fragmentA);
                    transaction.hide(fragmentC);
                    break;
                case 2:
                    transaction.show(fragmentC);
                    transaction.hide(fragmentA);
                    transaction.hide(fragmentB);
                    break;

            }
            transaction.commit();
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

@OnClick(R.id.btn_show_fragmentx)
public void showFragmentX() {
    if (fragmentX == null) {
        fragmentX = FragmentX.newInstance();
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, fragmentX, "X");
        transaction.addToBackStack("X");
        transaction.commit();
    } else {
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.show(fragmentX);
        transaction.addToBackStack("X");
        transaction.commit();
    }
}

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fragment);
        transaction.commit();

         getSupportFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}

}

1 个答案:

答案 0 :(得分:0)

调用onBackPressed时,您的fragmentX将被销毁。你需要检查backstack的当前片段,如果它是FragmentX的一个实例,然后隐藏它(并且不要弹出backstack),否则你的代码。