E
E
Eugene2016-03-31 21:25:49
Java
Eugene, 2016-03-31 21:25:49

How to pass link from activity to fragment?

Good day.
I met with such a problem that it is necessary to pass a link to a specific instance of the class (Bottom sheet if it plays a role) when creating, from the activity the link must be passed to the fragment.
At the moment of onViewCreated in the fragment, the link is nullified.
I tried through a setter, through a singleton - to do too much extra code for the sake of this listener.
The docs say that it is strictly forbidden to redefine a constructor.
Why is it necessary - in the fragment there is a view recycler, in the adapter of which I need to handle the event on button click.
vVPT.pngEW57.pngPu3c.png
ps screen with initBottomSheet from activity, the rest from the fragment.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-03-31
@Ragnar0k

In general, there are two ways to go:
1) Create some RecycleFragment.SomeEventListener interface with the onSomeEvent() method;
Oblige the activity to implement it, check it in onAttach. When you click on the button, send the event to the activity, where there is behavior and do everything you need there.
2) A similar scenario, if without behavior'a in the fragment, well, just not at all. Similar interface, we call it BehaviorProvider, the getBehavior() method returns behavior. Activity implements it and returns behavior on request. In the same way, check in onAttach, do not forget to remove the link.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question