Show DialogFragment from onActivityResult

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

    I know this was answered quite a while ago.. but there is a much easier way to do this than some of the other answers I saw on here... In my specific case I needed to show a DialogFragment from a fragments onActivityResult() method.

    This is my code for handling that, and it works beautifully:

    DialogFragment myFrag; //Don't forget to instantiate this
    FragmentTransaction trans = getActivity().getSupportFragmentManager().beginTransaction();
    trans.add(myFrag, "MyDialogFragmentTag");
    trans.commitAllowingStateLoss();
    

    As was mentioned in some of the other posts, committing with state loss can cause problems if you aren't careful... in my case I was simply displaying an error message to the user with a button to close the dialog, so if the state of that is lost it isn't a big deal.

    Hope this helps...

提交回复
热议问题