Search code examples
androidtabssavetabbedappcompatactivity

Save text fields content from Android Tabbed view


I am working on a corporate apps for internal use,

one of the sections that is presented to the user is a TabbedView (in fact I made it from the Android Studio "clean sheet", so its Fragment-based and extends AppCompatActivity), each one of the four tabs it is composed of contains a few text fields.

The user is free to navigate back and forth across the tabs and fill in the text fields, then hit "go" at the bottom of the last tab and submit the data from all the text fields. I am finding an incredibly hard time in doing this, what I would like to do is simply to use a listener telling me when the user is switching from a tab to the next one and save the data in the shared preferences. (being also able to restore it when the user is freely switch between tabs in the editing phase just doing the same thing with the listener for a tab selection)

I was quite surprised to see that the easy old tab system with the super handy listeners for "on tab selected" and "on tab deselected" event is now a thing of the past, so after two days spent trying to figure out how to do such a simple thing as saving a few text fields right before the tab gets killed I am now surrendering and asking for help. How can I automatically save data from the text fields in a tab before loading one of the others?

thanks! :-)


Solution

  • Read about fragment life cycle on the Android Fragment documentation page.

    Save your data in onPause.