-
HI, ` private SimpleDialog.OnDialogResultListener dialogResultListener = new SimpleDialog.OnDialogResultListener() {
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I am not sure what you want to achieve. The result listener interface is meant to be implemented in a fragment or activity context from where the dialog is shown, because the dialog will search for any fragment in its hierarchy up to the activity for this interface and call the result listener automatically (see wiki). A SimpleDialog is also a fragment so something like the following will work (see RecursiveDialog in test app): public class RecursiveDialog extends CustomViewDialog<RecursiveDialog>
implements SimpleDialog.OnDialogResultListener You can of course use the interface in any other possible way, but instantiating an interface is not possible in java. You can create and instantiate a custom class which implements the interface, but then you will have to call it yourself since the dialog has no way of knowing about it. You could do this e.g. by overwriting |
Beta Was this translation helpful? Give feedback.
-
So, if I create a variable (dialogResultListener) that is implement a SimpleDialog.OnDialogResultListener() I can not add to the SimpleDialogFragment? No method for it? For example: addResultListener(dialogResultListener))? |
Beta Was this translation helpful? Give feedback.
-
No, there is no You can google these keywords to dive into the topic or read e.g. this article, but basically it boils down to the following: every dialog has to be disposable and re-creatable via saved instance states at any time to handle situations where the user switches apps, get's a call, turns the screen off or simply rotates the phone. This is not always possible when you set a result listener callback as you suggested; and even if it's possible, it will result in situations where the result listener is associated with an activity that was destroyed, leading to frequent crashes due to invalid states. So by implementing the result listener as a method of the hosting activity or fragment, I can assure you that the results end up in the right place (the living activity or fragment), even when the dialog and/or activity or fragment was destroyed and recreated by Android since the time you called the dialog's show method. Internally this works by searching for this interface in the dialogs fragment hierarchy up to the hosting activity (see wiki). At the same time I can ensure that the dialog state (e.g. user input) is not lost in such cases. This being said, any information you need in order to process the dialog result should be passed via the extras bundle on dialog creation, so it is persistently delivered along with the result. As stated in the README:
|
Beta Was this translation helpful? Give feedback.
No, there is no
addResultListener
method, and this is for a very good reason (actually the reason this library came about in the first place): Android component lifecycles and configuration changes.You can google these keywords to dive into the topic or read e.g. this article, but basically it boils down to the following: every dialog has to be disposable and re-creatable via saved instance states at any time to handle situations where the user switches apps, get's a call, turns the screen off or simply rotates the phone. This is not always possible when you set a result listener callback as you suggested; and even if it's possible, it will result in situations where the result listener …