0

I am trying to do the following:

  1. Show dialog fragment from an Activity (not Fragment);
  2. Retrieve result in the Activity.

The problem is that onActivityResult in the Activity is not being called.

Here is how I am showing a DialogFragment

DriverRatingDialogFragment dp = new DriverRatingDialogFragment();
// this solution works well in case of showing dialog from a fragment
// but I have to show it from the Activity
// dp.setTargetFragment(getSupportFragmentManager().findFragmentById(R.id.flContent), DriverRatingDialogFragment.REQUEST_CODE);
dp.show(getSupportFragmentManager(), DriverRatingDialogFragment.ARG_RATING);

And here is how I am trying to return result from DialogFragment:

    Intent intent = new Intent();

    // This solution works well in case of previously setting target Fragment,
    // but I have to return result to Activity

    // getTargetFragment().onActivityResult(
    // getTargetRequestCode(), REQUEST_CODE, intent);


    // and with this attempt App crashes
    // this.onActivityResult(REQUEST_CODE, Activity.RESULT_OK, intent);

here is where I want to retrieve result in the Activity, but it doesn't get called:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == DriverRatingDialogFragment.REQUEST_CODE) {
            // todo

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

What's wrong?

Leo DroidCoder
  • 14,527
  • 4
  • 62
  • 54
  • Possible duplicate of [Receive result from DialogFragment](http://stackoverflow.com/questions/10905312/receive-result-from-dialogfragment) – tir38 Nov 22 '16 at 20:12

3 Answers3

1

I start the DialogFragment the same way you do.

CreatePostDialog createPostDialog = new CreatePostDialog();
createPostDialog.show(getSupportFragmentManager(), "create_post_dialog");

And I send back data using a public method in the activity rather than with intents.

((MainActivity)mContext).logout(Utils.getLocation(v));

The snippets above are copy pasted examples of my working code. Logout in this case is a public method in MainActivity that takes in a point and does what is needs with it.

Allan W
  • 2,791
  • 4
  • 23
  • 41
  • Cool, I haven't thought about it. Maybe it's not the best practice, but very handy in my case. Thanks! – Leo DroidCoder Jun 22 '16 at 17:52
  • 1
    I'm still new to coding but I've had friends of mine with more experience use similar methods. In the end, you can always check if the context is an instanceOf MainActivity prior to execution. – Allan W Jun 22 '16 at 18:54
  • 2
    Don't do this. You are coupling your Fragment to one Activity. Now only `MainActivity` can host CreatePostDialog(Fragment). What happens if you need that dialog elsewhere in your app? – tir38 Nov 22 '16 at 20:14
  • 2
    @tir38, if the dialog always calls a logout method, you could just make an interface for it, cast the context to the interface if the instance is correct, and then call it in the same way. My other alternative would be EventBus. – Allan W Nov 23 '16 at 06:32
  • @AllanW agreed. Those are much better options. – tir38 Nov 23 '16 at 19:16
1

you can use callback 1.define a callback in you dialog fragment

public Call mCall;
public interface Call {
        void returnData(Data data);
    }

2.on dialog fragment life circle

@Override
public void onDetach() {
    super.onDetach();
    mCall = (Call) getActivity();
    mCall.returnData(data);
}

3.activity just implements Call

activity implements DiaFragment.Call
王良海
  • 201
  • 3
  • 5
0

onActivityResult is there to receive informations after startActivityForResult is called, which is not your case.

What kind of datas you need to get from the dialog ?

DialogFragment is usually used in a fragment, not an activity. Look at alert dialog.

Alexandre Martin
  • 1,472
  • 5
  • 14
  • 27