Morton
Morton

Reputation: 5760

Remove fragment issue

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.

Upvotes: 1

Views: 398

Answers (1)

PEHLAJ
PEHLAJ

Reputation: 10126

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

Upvotes: 1

Related Questions