Recover Edittext from Fragments in Activity

Asked

Viewed 1,138 times

2

I have an Activity with Actionbar and three Tabs, in each Tab is loaded a different Fragment and in each Fragment of this I have a form.
When you finish filling the three forms, when you tap the save button I want to be able to recover the information of each Ragment and persist in Sqlite.

My question is to know how to access the fields of each Fragment.

I’ve tried using it

Fragment1 detalhes = (Fragment1) fragmentManager.findFragmentByTag("frag1");

but it only works to recover the field in the current Ragment. How do I recover from everyone?

  • To help you I need to see the code, in your activity, where the management and creation of the Tab's.

2 answers

0

A simple solution to keep data between Fragment transitions is from the method setOffscreenPageLimit(). Where is passed the amount of screens as parameter, in the example below the data will be kept between 3 screens.

Important to emphasize the greater memory consumption.

    viewPager.setOffscreenPageLimit(3);

0


When you move from one Ragment to the other you will have to send the data that has been filled out to the other to not lose.

For example, create a class with all form fields represented as attributes. When it comes to switching from Fragment A to B, you take all the data that was entered on the screen and play for the class you created. In Fragment B you will receive this data and will add more the data you have been informed now.

This way you can go forward or backward in the screen change and will always keep the data. When you arrive at the last Ragment, when you click Save, just take the transport class data and persist them.

  • It worked right here! I did it right and it worked perfect. Thanks for the help!

  • Now mine has arisen another doubt. I’m using Fragment’s onPause() method to take the informed data on the screen and fill this transport class. My problem is when I’m on the last fragment. Because I use a button in Actionbar to save and the method is in Activity, but since it is the last Fragment, the onPause() method of it has not been called, and so these data have not yet been passed to this transport class. Any ideas?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.