Show DialogFragment from onActivityResult

前端 未结 17 1262
傲寒
傲寒 2020-12-04 06:47

I have the following code in my onActivityResult for a fragment of mine:

onActivityResult(int requestCode, int resultCode, Intent data){
   //other code
   P         


        
17条回答
  •  无人及你
    2020-12-04 07:13

    There are two DialogFragment show() methods - show(FragmentManager manager, String tag) and show(FragmentTransaction transaction, String tag).

    If you want to use the FragmentManager version of the method (as in the original question), an easy solution is to override this method and use commitAllowingStateLoss:

    public class MyDialogFragment extends DialogFragment {
    
      @Override 
      public void show(FragmentManager manager, String tag) {
          FragmentTransaction ft = manager.beginTransaction();
          ft.add(this, tag);
          ft.commitAllowingStateLoss();
      }
    
    }
    

    Overriding show(FragmentTransaction, String) this way is not as easy because it should also modify some internal variables within the original DialogFragment code, so I wouldn't recommend it - if you want to use that method, then try the suggestions in the accepted answer (or the comment from Jeffrey Blattman).

    There is some risk in using commitAllowingStateLoss - the documentation states "Like commit() but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user."

提交回复
热议问题