Closed orange1988 closed 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();
The standard callbacks are invoked in 2 cases:
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).
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.
The listener depends on activity or fragment, but not context.
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.
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.
Thanks for your answers~
When I implement the interface with my Fragment in Activity, I can't receive the Callback.