Saving edit text, text views, radio buttons on fragment replace - android

I have an app where the user has to fill up a form. It has many text views, edit texts, and radio buttons.
If i replace that fragment later, how do I preserve the data if i go back to that replaced/previous fragment?

There are many options available. Try any of them.
Option 1: Use Shared Preferences
Saving Key-Value Sets
Option 2: Use Parcelable
Android Parcelable Example – Passing data between activities
Option 3: Use Fragment Save Instance State
Probably be the best way (?) to save/restore Android Fragment’s state so far

Related

how to get value (edit text, spinner and ...) from 3 different fragments into one JSON object or data class with good memory usage in android

I have 3 different layouts in Activity, with a lot of fields. right now I use Tab layout, ViewPager2, Fragments in my activity for load these 3 Fragments that contain too many fields (that in the end all of the text in these fields must aggregate into one JSON Object or data class). I disabled on touch event for Tab Layout and swipe for ViewPager and use 2 buttons in bottom layout activity (for validating current fragment's fields). these 2 buttons used for next, previous, back to home, and submit. (Actually, I have 2 buttons and handle text and functionality for these buttons with View Pager position). everything works correctly unit now except this, I need to aggregate all fields into one JSON or data class in these different Fragments as one JSON Object or data class.
my question is:
Is there a better way to handle this scenario or this one is good?
(Considering that, in the submit button, I must get all value from fields into 3 different fragments and save into one data class then send it into another Fragment or Activity and save into SQLite database with room and my problem is here too. how I must handle it for clean code and good performance)
thanks for your help

ViewPager Fragment Views retaining values from previous state

I have a 3-tab ViewPager with custom Fragments (EntryFragment and CalendarFragment; the third is not relevant to the question). Now when I click a date on CalendarFragment the EntryFragment should load up with data of the new date (I have coded it this way).
Now, what happens is something strange: The TextFields in EntryFragment get changed to the new data from the new date. But the Seekbars, Spinners, Switches, etc retain data from their previous date.
I am using the following methods to set the values of the various Views:
seekbar.setProgress(int);
spinner.setSelection(int, false);
switch.setChecked(boolean);
Also, I have attached onItemSelectedListerners for these components. These are getting called automatically after onCreateView() of EntryFragment.
Could anyone guide me why this is happening? Or how to prevent it?
I'm guessing it might be restoring previous state, ViewPagers have inbuilt mechanism to restore views that had been hidden. Your data is set to correct values and restored to previous state after that. To prevent that, try to add saveEnabled=false in xml layout of views you are having problem with.

How can I save the state of an activity in Android

I want to save the sate of an activity even if it is destroyed, Means if i have a lot of controls on the activity view then when ever changes occur on the controls after activity is called like selected any options on the spinner or the TextView text is changed etc I can save each control state separately but I want to save the whole activity object type thing so if activity recreated then all the options on the controls are selected at once. So that there is less coding. and If I perform any animation on any control then its changed position are saved if any animation is applied on that control.
Just use Shared Preferences. Its that simple.
Refer : http://developer.android.com/reference/android/content/SharedPreferences.html
I have used Shared preferences with Gson and saved the whole object as string at once. IT is not I wanted but It helped to save the state in ore less way. Thanks for all to reply.

Fragment save state on configuration change

I am developing twitter client.
I have TimelineFragment with loaders which load data from db and web.
I have setRetainInstance(true) in onActivityCreated.
When orientation change view recycled? how i can prevent this?
It depends on what you are trying to save. Your member variables will be saved but you will have to use them to reinitialize your view widgets such as TextView, EditText, Buttons.
Alternatively if its simple data you want to save such as text on TextViews, EditText, Buttons, etc.., you can use an xml feature on them called "freezesText"

How to change Text in EditTexts in other tabs

Afternoon guys, my problem is:
I have a main tab that contain a 'Search' button, when it's pressed it should load data from the database (which is OK) and apply it to all the tabs (here's the problem)
How an i supposed to do this, is it possible?
If it's not possible, is there any way to pass info trought tabs? Like the customer ID so i could search his info whenever the tab changes?
Thx
Ye is possible. Save the content in one global variable and use it in the next tab. In your case if it is string save declare one private/public string variable outside the tabs, when is needed assign data to the string, and use it in the same in the next tabs or whatever you want.

Categories

Resources