Search code examples
javaandroidandroid-fragmentsandroid-fragmentactivity

Android - manage multiple fragments form in one activity


Let`s say I have to input an order through 4 kinds of form, each form as a fragment and all fragments is inside an activity

what is the best way to manage data for each fragments considering it will be able to next, and go back to the previous fragment (restore previous input) ?

I have done quite a bit research, and found 2 ways 1. using a singleton class 2. using onsaveinstance to manage and restore data through several fragments


Solution

  • Singletons are a great way to store data temporarily in an Android app. The right solution depends on what you are trying to do though. The nature of a singleton is that there can only be one instance of it. This means the user will not be able to fill out the forms more than once unless you either are updating the current singleton values or you create different singleton objects (not new instances of the first singleton object) to store the new information.

    If this is not a concern of yours, I would suggest using the singleton. I regularly use them with great success. Big Nerd Ranch promotes their use and wrote a whole chapter on them in their Android Programming book.

    If the singleton doesn't work I would suggest passing the values to the fragment's parent activity and keeping them there rather than in a bundle onSavedInstance.