Android glasses of fragments



  • Help solve the problem of fragments.

    What's the problem:

    There are fragments - ABC

    I'm moving from the fragment. A - Category B with the possibility of a return (addToBackStackusing the function replace

    Next, I need to move from a fragment. B - Category C without a return, using a function replace

    From the fragment C The user should not return on the button back, but if he so desires, he can do it if he does a couple of actions on the fragment and the return has to go to the fragment. A♪ I'm programmeally returning the user from the fragmette-- C - Category A missing fragment B

    If briefly, it is:

    A - constituent B [replace, addToBackStack]
    B - tie C [replace]
    C - tie A [replace]

    It's okay until I press the back button on the phone when I'm in a fragment. A after transition C♪ The annex makes a mistake about a breach in the glass.

    Here.

    12-18 16:18:47.381 4116-4116/projectname E/InputEventSender: Exception dispatching finished signal.

    12-18 16:18:47.382 4116-4116/projectname E/MessageQueueue-JNI: Exception in MessageQueueue callback: handleReceiveCallback

    12-18 16:18:47.406 4116-4116/projectname E/MessageQueue-JNI: java.lang.IllegalStateException: Fragment already added: fragment_logo{a9d527a #1 id=0x7f0e00b8 fragment_logo}

    12-18 16:18:47.407 4116-4116/projectname E/AndroidRuntime: FATAL EXCEPTION: main Process: projectname, PID: 4116 java.lang.IllegalStateException: Fragment already added: fragment_logo{a9d527a #1 id=0x7f0e00b8 fragment_logo} at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1319) at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:916) at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1666) at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:586) at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:169) at main.onBackPressed(main.java:656) at android.app.Activity.onKeyUp(Activity.java:2483) at android.view.KeyEvent.dispatch(KeyEvent.java:2664) at android.app.Activity.dispatchKeyEvent(Activity.java:2736) at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50) at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:224) at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2310)

    UPD:

    Transitions:

    [A] - REQUIREMENTS FOR THE USE OF ANNEXES

    public void ShowLoginFragment() {
            fragmentmanager = getSupportFragmentManager();
            fragmenttransaction = fragmentmanager.beginTransaction();
            fragmenttransaction.setCustomAnimations(R.anim.alpha, R.anim.alpha_end);
            fragmenttransaction.replace(R.id.layout_main, fragment_logo, fragment_logo.TAG);
            fragmenttransaction.commit();
    }
    

    TRANSITION TO FRAGMN [B] - TO THE CENSUS

    fragmentmanager = getSupportFragmentManager();
    fragmenttransaction = fragmentmanager.beginTransaction();
    fragmenttransaction.setCustomAnimations(R.anim.slide_left_to_right, R.anim.slide_right_to_left);
    fragmenttransaction.replace(R.id.layout_main, fragment_new_order_not_reg, fragment_new_order_not_reg.TAG)
                       .addToBackStack(fragment_new_order_not_reg.TAG);
    fragmenttransaction.commit();
    

    [C]

    fragmentmanager = getSupportFragmentManager();
    fragmenttransaction = fragmentmanager.beginTransaction();
    fragmenttransaction.setCustomAnimations(R.anim.alpha, R.anim.slide_right_to_left);
    fragment_new_order_status.callbackResume = callbackResume;
    fragmenttransaction.replace(R.id.layout_main, fragment_new_order_status, fragment_new_order_status.TAG);
    fragmenttransaction.commit();
    

    So I'm transmitting a callback class to which I'm going from a fragment [C] to reset the fragment [A] - ShowLoginFragment()

    fragment_new_order_status.callbackResume = callbackResume;
    

    I tried to do the same, but no.

    .addToBackStack(fragment_new_order_not_reg.TAG);
    

    It's all right to work after returning to Fragment A, when you press the button back on the phone, the app just turns around. But then the user won't be able to get back from the fragment [B] and need it.



  • Got it.

    First - If inside the fragments you load other fragments, you better use - getChildFragmentManager()

    Second

    Added the clean-up before loading a fragment that interrupts the chain of return.

    private void clearStack(){
            int count = fragmentmanager.getBackStackEntryCount();
            while(count > 0){
                fragmentmanager.popBackStack();
                count--;
            }
        }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2