0

i its probably a basic question, but i cant figure it out with google.

When i create a new Fragment in Android Studio, it provides an interface named "onFragmentInteractionListener" which has to be implemented by the Activity that contains this Fragment. The Fragment also provides an "onButtonPressed" method that calls the onFragmentInteraction(Uri uri) method in the Interface.

But i have no idea how to use it. How to get a Button Click Event from the Fragment to the Activity? I let the ButtonView call the onButtonPressed method. But how does it continue from there? And what does URI mean here? I googled it, but i dont understand it in the context of this problem.

Florian Walther
  • 6,237
  • 5
  • 46
  • 104
  • Unclear why a URI is needed, but here's that question. https://stackoverflow.com/questions/176264/what-is-the-difference-between-a-uri-a-url-and-a-urn – OneCricketeer Aug 09 '17 at 00:20
  • I saw that other question, but no answer there explains how to use it. Just how and why to implement it (what i already did) – Florian Walther Aug 09 '17 at 00:22
  • It's an interface, so there's no other way to use it than implement and call it – OneCricketeer Aug 09 '17 at 00:24
  • I have that same interface in every fragment and 1 time in the activity. I also have more than 1 button in every fragment. How do i get the button click to the Activity? I dont understand it, thats why i asked this question. – Florian Walther Aug 09 '17 at 00:25
  • The URI parameter can be changed to whatever data needs to be sent back to the Activity, and you can define multiple interfaces. The default name can be overriden too. Reading over the full example here should clarify why you'd use it, but it's not required. https://developer.android.com/training/basics/fragments/communicating.html – OneCricketeer Aug 09 '17 at 00:26
  • You only need one interface defined if all Fragments send the same data to the Activity – OneCricketeer Aug 09 '17 at 00:27
  • Ok thank you, ill try to understand it tomorow with the link you sent me. Its already past 2am here. – Florian Walther Aug 09 '17 at 00:28
  • Welcome, and good luck. Regarding the question here, the 'onFragmentInteraction(uri)` method of the interface does go to the Activity (or any class that implemented that interface), but that's setup in onAttach to the Activity. It's the recommended way to get data to the Activity rather than calling `getActivity()` followed by a method call – OneCricketeer Aug 09 '17 at 00:31

0 Answers0