Show DialogFragment from onActivityResult

前端 未结 17 1191
傲寒
傲寒 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:11

    onActivityResult() executes before onResume(). You need to do your UI in onResume() or later.

    Use a boolean or whatever you need to communicate that a result has come back between both of these methods.

    ... That's it. Simple.

    0 讨论(0)
  • 2020-12-04 07:12

    EDIT: Yet another option, and possibly the best yet (or, at least what the support library expects...)

    If you're using DialogFragments with the Android support library, you should be using a subclass of FragmentActivity. Try the following:

    onActivityResult(int requestCode, int resultCode, Intent data) {
    
       super.onActivityResult(requestCode, resultCode, intent);
       //other code
    
       ProgressFragment progFragment = new ProgressFragment();  
       progFragment.show(getActivity().getSupportFragmentManager(), PROG_DIALOG_TAG);
    
       // other code
    }
    

    I took a look at the source for FragmentActivity, and it looks like it's calling an internal fragment manager in order to resume fragments without losing state.


    I found a solution that's not listed here. I create a Handler, and start the dialog fragment in the Handler. So, editing your code a bit:

    onActivityResult(int requestCode, int resultCode, Intent data) {
    
       //other code
    
       final FragmentManager manager = getActivity().getSupportFragmentManager();
       Handler handler = new Handler();
       handler.post(new Runnable() {
           public void run() {
               ProgressFragment progFragment = new ProgressFragment();  
               progFragment.show(manager, PROG_DIALOG_TAG);
           }
       }); 
    
      // other code
    }
    

    This seems cleaner and less hacky to me.

    0 讨论(0)
  • 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."

    0 讨论(0)
  • 2020-12-04 07:14

    I got this error while doing .show(getSupportFragmentManager(), "MyDialog"); in activity.

    Try .show(getSupportFragmentManager().beginTransaction(), "MyDialog"); first.

    If still not working, this post (Show DialogFragment from onActivityResult) helps me to solve the issue.

    0 讨论(0)
  • 2020-12-04 07:14

    Another way:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case Activity.RESULT_OK:
                new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(Message m) {
                        showErrorDialog(msg);
                        return false;
                    }
                }).sendEmptyMessage(0);
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }
    
    
    private void showErrorDialog(String msg) {
        // build and show dialog here
    }
    
    0 讨论(0)
提交回复
热议问题