2

I have 3 fragment fragmentA, fragmentB, fragmentC that are added by default on the activity on create to keep it instance after that, I have another fragment fragmentX that to be added on the fragment container with backStack on Button click, the fragmentX is shown on add but when I need to show it again its not working.

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();
    }
}

}

TABZ
  • 71
  • 9
  • if the fragmentX is null then I initialized it on the if statement before I added to the transaction. and I need to keep track of the fragment for handling the back press button click so I don't need to remove the addToBackStack function – TABZ May 28 '18 at 03:49
  • add `.addToBackStack(null)` should work – Kopi Bryant May 28 '18 at 03:55
  • adding the fragment works fine but when I need to show it again and the fragment is not null I just need to show it again on the else condition. – TABZ May 28 '18 at 03:59
  • When you backstack the fragment will remove from the stack. if you want to show this again you have to add the fragment again in the backstack – Vinayak B May 28 '18 at 04:37
  • You should use `ViewPager` for using fragments with tabs – Hanzala May 28 '18 at 05:31

1 Answers1

0

You fragmentX is destroyed when onBackPressed is called. You need to check for the backstack's current fragment, if it is an instance of FragmentX, then hide it (and don't pop the backstack), otherwise do your code.

Adib Faramarzi
  • 3,798
  • 3
  • 29
  • 44