Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
orange1988 opened this issue Mar 15, 2016 · 7 comments

Comments

@orange1988
Copy link

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

@TomasKypta
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Author

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

@orange1988
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

Thanks for your answers~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants