avast / android-styled-dialogs

Backport of Material dialogs with easy-to-use API based on DialogFragment
Apache License 2.0
2.15k stars 450 forks source link

When I implement the interface with my Fragment in Activity, I can't receive the Callback. #134

Closed orange1988 closed 8 years ago

orange1988 commented 8 years ago

When I implement the interface with my Fragment in Activity, I can't receive the Callback.

TomasKypta commented 8 years ago

The library supports receiving callbacks in both Fragment and Activity.

To receive the callback in a Fragment don't forget to call setTargetFragment(MyFragment.this, MY_REQUEST_CODE) on the builder.

So create a simple dialog this way:

SimpleDialogFragment.createBuilder(getActivity(), getFragmentManager())
            .setTitle(R.string.some_title)
            .setMessage(R.string.some_message)
            .setNegativeButtonText(android.R.string.cancel)
            .setPositiveButtonText(android.R.string.ok)
            .setTargetFragment(this, MY_REQUEST_CODE)
            .show();
TomasKypta commented 8 years ago

The standard callbacks are invoked in 2 cases:

  1. The activity the fragment is assigned to implements the interface.
  2. The target fragment implements the interface.

Both listeners in activity and fragment can be invoked.

If you implemented a custom dialog fragment and/or a custom listener you have to take care of it yourself (check source code).

orange1988 commented 8 years ago

I'm sorry that I can't express my situation before. In fact, when I want to use it in a view, it doesn't work.

orange1988 commented 8 years ago

The listener depends on activity or fragment, but not context.

orange1988 commented 8 years ago

Although I don't want to use it in this way, but my team show dialog in a custom view in many places. So I have to solve this problem.

TomasKypta commented 8 years ago

Unfortunately, the listeners can't be used in a custom view this way, the library currently doesn't support it. This wasn't implemented because of many potential memory leaks. View is bound to context and thus to the current activity. Screen rotation would cause memory leaks. Or it would have to be weakly held but in that case the reference disappears when you rotate the device.

But if you write a bit of code, you can achieve the desired behaviour by propagating the callbacks from fragment/activity into your custom views.

orange1988 commented 8 years ago

Thanks for your answers~