Show DialogFragment from onActivityResult

前端 未结 17 1269
傲寒
傲寒 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: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;
        }
    }
    

提交回复
热议问题