Fragment save state on configuration change - android

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"

Related

How does one save the current view in android?

The design of the application that I'm working on is fairly simple so I've decided to use Activity.setContentView() to switch between the few different layouts it has. This all happens within a single Activity, of course.
To explain it better - let's say that I have a main layout with a simple navigation and a settings button.
The problem is that whenever I show the settings view upon click and later try to set the main view back, it is unusable. It just loads the main layout, but no listeners are set, as if I show a picture.
I've figured out that I should use the setContentView(View view) constructor instead of the one that passes the layout id from the resources. Though, I have no idea how to save the current view..
How to save the current view with all its listeners and stuff to then pass it to the constructor and save my data?

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.

How to restore dynamic views to the android app?

I am creating dynamic views, Adding them to the layout those views are adding fine.But once i close the app and open it Dynamic views are not visible,I want to display those views after opening the app second time. Will you give me any idea, Thanks in advance.
You'll have to manually add the view's state to the saved instance state bundle, and recreate it from there.
This is accomplished by overriding onSaveInstanceState() on your Activity and/or View. The state will be later available in the creation Bundle (onCreate(bundle)). You can use that data to re-instantiate your views.
The correct OOP way of things would be to create a class for your dynamically created view that implements a custom onSaveInstanceState() method, and then packing it and unpacking it into the Activity's bundle.
This blog post contains details and examples on how to tackle the issue. Some more related info in this other SO question

Android TabActivity and dynamic Tabs screen orientation issue

I am adding Views (not Activities) to a TabActivity on demand (meaning that the activity can have zero or more tabs after onCreate is done). Over the time more tabs get added, now I need to save them in a sane way so I can restore them later on. I tried to use saveHierarchyState etc on the individual views, but they are restored without data from the editTexts etc.
So given that I have a HashMap in the activity which maps from tabkeys to views, how would you save this data and restore it?
Okay, I got it :)
First I thought I needed to set the IDs of the Views as described here -- using this approach Android is able to restore the view states. But since my dynamic tabs all use views from the same layout their children all have the same IDs, so android will overwrite the data (The state is saved in a sparse array according to the view ID, so the IDs should be unique) and all views look the same. I fixed that by manually saving the state of my views and reapplying it via:
SparseArray<Parcelable> container = new SparseArray<Parcelable>();
saveHierarchyState(container);
b.putSparseParcelableArray("state", container);
restoreHierarchyState(b.getSparseParcelableArray("state"));
While this approach works I still have to find out if I can let Android do all the work by not setting IDs for the Views in the layout xml file (if that's allowed/possible).

Categories

Resources