Home > OS >  How to transfer the activity and fragment information to the server with a button in the activity?
How to transfer the activity and fragment information to the server with a button in the activity?

Time:09-07

I have an activity that contains several edit texts and a view pager And inside the view pager there are some text edits

Now, I want when the user fills the form inside the activity and the form inside the fragment, all the information will be transferred to the server by prenter image description hereessing one button.

The problem is that the button is in the main activity and I don't have access to edit the texts inside the fragment

What should I do ???

CodePudding user response:

As your Activity is in control of the Fragment, you could just implement a custom method inside the Fragment like fun getFormInput(): Map<String, String> or similar.

When the "send" button is clicked, you use that method to receive all information from the Fragment.

Alternatively, you could implement a custom listener, with the observer pattern, to be notified inside the Activity on every change of a form field in the Fragment.

CodePudding user response:

You can add an interface that will return you the map object as mentioned above.

Create a method in the interface that returns the data stored in all the dit texts in any Data Structure that best fits your use case.

In your fragment - add a watcher to all your edit texts and send the values present in the edittexts to the activity using the method in the interface.

  • Related