Show DialogFragment from onActivityResult

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

    just call super.onActivityResult(requestCode, resultCode, data); before handling the fragment

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

    It's an old question but I've solved by the simplest way, I think:

    getActivity().runOnUiThread(new Runnable() {
        @Override
            public void run() {
                MsgUtils.toast(getString(R.string.msg_img_saved),
                        getActivity().getApplicationContext());
            }
        });
    
    0 讨论(0)
  • 2020-12-04 07:07

    It happens because when #onActivityResult() is called, the parent activity has already call #onSaveInstanceState()

    I would use a Runnable to "save" the action (show dialog) on #onActivityResult() to use it later when activity has been ready.

    With this approach we make sure the action we want to will always work

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == YOUR_REQUEST_CODE) {
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    showDialog();
                }
            };
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
    
    @Override
    public void onStart() {
        super.onStart();
        if (mRunnable != null) {
            mRunnable.run();
            mRunnable = null;
        }
    }
    
    0 讨论(0)
  • 2020-12-04 07:09

    If you use Android support library, onResume method isn't the right place, where to play with fragments. You should do it in onResumeFragments method, see onResume method description: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#onResume%28%29

    So the correct code from my point of view should be:

    private boolean mShowDialog = false;
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
      super.onActivityResult(requestCode, resultCode, data);
    
      // remember that dialog should be shown
      mShowDialog = true;
    }
    
    @Override
    protected void onResumeFragments() {
      super.onResumeFragments();
    
      // play with fragments here
      if (mShowDialog) {
        mShowDialog = false;
    
        // Show only if is necessary, otherwise FragmentManager will take care
        if (getSupportFragmentManager().findFragmentByTag(PROG_DIALOG_TAG) == null) {
          new ProgressFragment().show(getSupportFragmentManager(), PROG_DIALOG_TAG);
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-04 07:09

    I believe it is an Android bug. Basically Android calls onActivityResult at a wrong point in the activity/fragment lifecycle (before onStart()).

    The bug is reported at https://issuetracker.google.com/issues/36929762

    I solved it by basically storing the Intent as a parameter I later processed in onResume().

    [EDIT] There are nowadays better solutions for this issue that were not available back in 2012. See the other answers.

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

    I came up with a third solution, based partially from hmt's solution. Basically, create an ArrayList of DialogFragments, to be shown upon onResume();

    ArrayList<DialogFragment> dialogList=new ArrayList<DialogFragment>();
    
    //Some function, like onActivityResults
    {
        DialogFragment dialog=new DialogFragment();
        dialogList.add(dialog);
    }
    
    
    protected void onResume()
    {
        super.onResume();
        while (!dialogList.isEmpty())
            dialogList.remove(0).show(getSupportFragmentManager(),"someDialog");
    }
    
    0 讨论(0)
提交回复
热议问题