B
B
BitNeBolt2020-09-08 15:26:30
Java
BitNeBolt, 2020-09-08 15:26:30

Should I set the listener this way?

There is a listener interface that implements an activity or a fragment (or something else). Events occur in another activity or fragment that should call listener methods.

Is it ok to use a static setter (if the object is guaranteed to be in one instance) for a static listener and check for null before calling the method?

How should the listener be set if (conditionally) the fragment must be created in the adapter, or instances of the same fragment must be used multiple times?

PS Perhaps I formulated something too clumsily

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2020-09-08
@BitNeBolt

Short answer: No.
Long answer:
Communication between different activities should be done using Intents, startActivityForResult, onActivityResult.
Communication between a fragment and an activity should be done through fragment arguments, searching by tag/id and casting the fragment to a specific interface, getActivity() and casting the activity to a specific interface.
Communication between fragments lying on the same level (lying in the same FragmentManager'e) should occur through the activity and interfaces (fragment A asks the activity for interface B, the activity looks for fragment C that implements interface B and provides it), or through the TargetFragment mechanism (see get/setTargetFragment).
Communication between fragments that lie in different fragment managers and are related by a parent-child relationship (see getChildFragmentManager) must occur through getParentFragment + interfaces.
All this is quite complicated and confusing, but here is such an API.

A
Alexey Cheremisin, 2020-09-08
@leahch

Of course, I'm not an android programmer, but in my projects (just Java and nothing else) I use https://greenrobot.org/en-eventbus/
In my opinion, it's much nicer than using Intents if they are not really needed.
Well, for traditional Java - there are several dispatching mechanisms, including through the master thread. Well, do not reinvent your wheel. I used to use guava eventbus, but this one is better.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question