سؤال

I have one activity in which I load all details of my application by loading different different fragment in my activity's frame-layout.

In case of simple flow, when I load A > B > C > D and then coming back in D > C >B >A is working fine as expected. I always want to move from B to A when back is pressed.

But now in one instance, I have different seq. in which I load A > B > C and then from C I have one button from which I need to load B again. Now when user press back on B, I need to go to A again without going to C. So to achieve that,in onKeyDown event of the parent activity, I checked that if the current fragment is my B fragment then just load A fragment and clear the backstack by popBackStackImmediate() (as I don't need it anymore). But now when I press back on B then it does load fragment A but the fragment B is not removed. I also tried to check for several answers for removing and adding fragments manually but still no luck. If you have any suggestions for that also then please explain me that process of add and remove or give me some reference site location where I can get details.

I tried to figure out the exact working of backstack but in that also, no luck. I don't understand what's going on (Why that fragment B is not removed and getting added to A). I have been trying for this from last two days. Please help me out.

Thanks a lot in advance.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Log.e(LOG_TAG, "Key down = " + keyCode);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (getSupportFragmentManager().findFragmentByTag(
                    RefillMainScreen.FRAGMENT_TAG) != null) {
                RefillMainScreen refillMainScreen = (RefillMainScreen)                                                                                                                           getSupportFragmentManager()
                        .findFragmentByTag(RefillMainScreen.FRAGMENT_TAG);
                if (refillMainScreen.isVisible()) {
                      MainActivity.displayBackStack(getSupportFragmentManager());
                    while (getSupportFragmentManager().getBackStackEntryCount() > 1) {
                        getSupportFragmentManager().popBackStackImmediate();
                    }
                    loadFragment(new DashboardFragment(), false);

                } else {
                    super.onKeyDown(keyCode, event);
                }
            } else {
                super.onKeyDown(keyCode, event);
            }
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
هل كانت مفيدة؟

المحلول

On the button Click where you wish to from C - A simply pop the back stack as

public void onClick(View v){
   fragmentManager.popBackStack();
}

This will pop C from the back stack i.e. take it out or remove and you will go back to B and since you would have added B to back stack while coming from A, on onBackPress from B you will be taken back to A, the way you want. :)

نصائح أخرى

Don't call the method addToBackStack() when you are moving from C to B. This way, the transaction won't be remembered.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top