Actions in onActivityResult and “Error Can not perform this action after onSaveInstanceState”

前端 未结 7 500
情话喂你
情话喂你 2020-12-13 04:41

Implementing an app where the user can log in I have the following situation: If the user is logged in perform the action else start the login activity for result and if the

相关标签:
7条回答
  • 2020-12-13 04:45

    Override show(Fragment manager, String tag) function with allowing state lose and change mDismissed = false; mShownByMe = true; from origibal function by reflection:

    public class DialogParent extends DialogFragment {
    
        @Override
        public void show(FragmentManager manager, String tag) {
            try {
                Field mDismissed = DialogFragment.class.getDeclaredField("mDismissed");
                Field mShownByMe = DialogFragment.class.getDeclaredField("mShownByMe");
                mDismissed.setAccessible(true);
                mShownByMe.setAccessible(true);
                mDismissed.setBoolean(this, false);
                mShownByMe.setBoolean(this, true);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:47

    I simply check whether an activity is being destroyed.

    if (!getActivity().isFinishing()) {
        DialogFragment fragment = MyFragment.newInstance();
        fragment.show(getActivity().getSupportFragmentManager(), MyFragment.TAG);
    }
    

    See also https://stackoverflow.com/a/41813953/2914140. In DialogFragment write:

    override fun show(manager: FragmentManager?, tag: String?) {
        try {
            val ft = manager?.beginTransaction()
            ft?.add(this, tag)
            ft?.commitAllowingStateLoss()
        } catch (ignored: IllegalStateException) {
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:50

    This real works.

    CheckinSuccessDialog dialog = new CheckinSuccessDialog();
    //dialog.show(getSupportFragmentManager(), null);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(dialog, null);
    ft.commitAllowingStateLoss();
    

    But but still bad, because got error “Activity has been destroyed”

    ava.lang.IllegalStateException: Activity has been destroyed fragmentTransaction.commitAllowingStateLoss();
    

    So my solution is add check if (!isFinishing()&&!isDestroyed())

    CheckinSuccessDialog fragment = CheckinSuccessDialog.newInstance();
    
         if (fragment instanceof DialogFragment) {
                    DialogFragment dialog = (DialogFragment) fragment;
                    if (!dialog.isAdded()) {
                        fragmentTransaction.add(dialog, 
                              CheckinSuccessDialog.class.getName());
                        if (!isFinishing()&&!isDestroyed()) {
                            fragmentTransaction.commitAllowingStateLoss();
                        }
                    }
    

    on dismiss:

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            Fragment fragment = getSupportFragmentManager().findFragmentByTag(CheckinSuccessDialog.class.getName());
            if (fragment != null && fragment instanceof DialogFragment) {
                DialogFragment dialog = (DialogFragment) fragment;
                dialog.dismiss();
                if (!isFinishing()&&!isDestroyed()) {
                    fragmentTransaction.commitAllowingStateLoss();
                }
            }
    
    0 讨论(0)
  • 2020-12-13 04:52

    Here is the workaround that works fine for me.

    private void alert(final String message) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            public void run() {
                AlertDialogFragment alertDialogFragment = AlertDialogFragment.newInstance(message);
                alertDialogFragment.show(getFragmentManager(), ALERT_DIALOG_FRAGMENT);
            }
        });        
    }
    
    0 讨论(0)
  • 2020-12-13 04:59

    Best thing I've come up with is to not use .show() but rather do this.

    CheckinSuccessDialog dialog = new CheckinSuccessDialog();
    //dialog.show(getSupportFragmentManager(), null);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(dialog, null);
    ft.commitAllowingStateLoss();
    
    0 讨论(0)
  • 2020-12-13 05:03

    If using a DialogFragment, the only thing that worked for me was to dismiss the Fragment with dissmissAllowingStateLoss()

    0 讨论(0)
提交回复
热议问题