1

I have three fragments :

A is my main fragment, B is a login fragment, successful login will enter C fragment.

When I click on back button, I need to move to Fragment A from Fragment C.

My issue is that, when I click on back button I still move on Fragment B from Fragment A.

How can I fix my issue?

Here is my switch fragment function:

public void switchFragment(Fragment fragment) {
    FragmentManager manager = getActivity().getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.mainFrame, fragment, null);
    transaction.addToBackStack(null);
    transaction.commit();
}

A fragment(NewHomepage) to B(LoginFragment) fragment:

switchFragment(LogInFragment.newInstance());

This is my B fragment, it has the value logged to decide switch A fragment or not when it come from C fragment.

I think that issue must be here, when go back to A fragment and click back button want to quit the APP, I can see the logcat show 1=> and 2=> .

String logged = memberData.getUD_MBTYPENAME(); //get the value when login succeed 
        Log.d(TAG,"1=>"+logged);
        //If UD_MBTYPENAME is not null,change to A fragment
        if (!TextUtils.isEmpty(logged)) {
            Log.d(TAG,"2=>"+logged);
            ((MainActivity) getActivity()).switchFragment(NewHomepage.newInstance());
        }

Here is about my MainActivity about onKeyDown and switchFragment:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

        if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
            quickDialog();//It's a alert dialog
            return false;
        }
    }
    return super.onKeyDown(keyCode, event);
}

public void switchFragment(Fragment fragment) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.mainFrame, fragment, null);
    transaction.addToBackStack(null);
    transaction.commit();
}

Take a reference change the code like this , if I don't use transaction.addToBackStack(null); , the issue is still there , even though remove transaction.addToBackStack(null);

When I back to A , I have to click twice back to show the alert dialog, I don't know what happened when I click back first time in A fragment.

if (!TextUtils.isEmpty(logged)){
        Log.d(TAG,"2=>"+logged);

        hideFragment(LogInFragment.newInstance());
        switchFragment(NewHomepage.newInstance());
    }

public void switchFragment(Fragment fragment) {
    FragmentManager manager = getActivity().getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.mainFrame, fragment, null);
    //remove it will fix my issue , but I have to click back twice to show alert dialog , I don't know what happened click it first time in A fragment.
    //transaction.addToBackStack(null);
    transaction.commit();
}

public void hideFragment(Fragment fragment) {
    FragmentManager manager = getActivity().getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.hide(fragment);
    transaction.commit();
}

remove hideFragment and use manager.popBackStack(); on switchFragment , the issue will be fixed.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Morton
  • 5,380
  • 18
  • 63
  • 118
  • 3
    Possible duplicate of [How to delete a specific fragment from back stack in android](http://stackoverflow.com/questions/31198894/how-to-delete-a-specific-fragment-from-back-stack-in-android) – Mohammad Tauqir May 09 '17 at 03:01
  • Hey thanks for your help , i can find the answer from your link. Thank you very much. – Morton May 09 '17 at 03:14

1 Answers1

1

Try

It will show fragment if it is already added.

Use fragmentTransaction.show method to re-use existing fragment i.e. saved instance.

public void switchFragment (Fragment oldFragment, Fragment newFragment, int frameId) {

    boolean addFragment = true;

    FragmentManager fragmentManager = getFragmentManager ();
    String tag = newFragment.getArguments ().getString ("TAG");
    Fragment fragment = fragmentManager.findFragmentByTag (tag);

    // Check if fragment is already added
    if (fragment != null && fragment.isAdded ()) {
        addFragment = false;
    }

    // Hide previous fragment
    String oldFragmentTag = oldFragment.getArguments ().getString (BaseFragment.TAG);

    if (!tag.equals (oldFragmentTag)) {
        FragmentTransaction hideTransaction = fragmentManager.beginTransaction ();
        Fragment fragment1 = fragmentManager.findFragmentByTag (oldFragmentTag);
        hideTransaction.hide (fragment1);
        hideTransaction.commit ();
    }

    // Add new fragment and show it
    FragmentTransaction addTransaction = fragmentManager.beginTransaction ();

    if (addFragment) {
        addTransaction.add (frameId, newFragment, tag);
        addTransaction.addToBackStack (tag);
    }
    else {
        newFragment = fragmentManager.findFragmentByTag (tag);
    }

    addTransaction.show (newFragment);

    addTransaction.commit ();
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • Thanks for your code man , so do it means that when C to A just hide B from your code in my case ? – Morton May 09 '17 at 07:33
  • It will hide B it oldFragment refers to B – PEHLAJ May 09 '17 at 07:34
  • Yes, when you switch from B to another fragment, it hides B – PEHLAJ May 09 '17 at 07:35
  • thanks for your responding , but i find that if i don't remove `transaction.addToBackStack(null);` , the issue won't be fixed even though i use hide or not. I had updated the question. – Morton May 09 '17 at 08:03