Persisting information of a fragment from a ViewPager - android

A fragment has a text field whose information has to be persisted when the user scrolls from page to page. Saving the value of the text field to the model from TextWatcher.onTextChanged() is too early.
In which lifecycle method of the fragment would I need to persist the data to the model? I tried onSaveStateInstance() and onPause(). Neither of them gets called when the user scrolls from one page (fragment) to the other.
Thank you

You could try using a FragmentStatePagerAdapter instead of just a FragmentPagerAdapter. Then persist the information in each Fragment's onSaveInstanceState() event.

Related

how ViewPager2 handle fragments

In my app I have a ViewPager2, the fragment inside this ViewPager have some code on onViewCreated. I have 2 question:
If I update the dataset and then return to old one the onViewCreated is not called, because of fragment cache, how can I intercept the re-show? I am ok with cache, bui I want restore the initial focus of the fragment when re-showed.
How can I intercept the hiding from the fragment? My fragment show a dialog, then an external event can change the data set, I want in this case dismiss the dialog.
Are 2 days that I am trying to fix those 2 issues.
EDIT: I solved the 2 observing the changing of data set.
Data sharing by ViewModel looks like a good solution for both cases. Fragments will rely on data change in shared ViewModel (depends on the structure of your screen it can be data sharing with host activity or parent fragment).
If it's not what you are looking for, provide more details.

Saving state of Fragments in a Viewpager

Okay I know this has been asked previously but just hear me out !
ISSUE
I want to save state of a fragment and then retrieve it in onCreateView method .
I have a ViewPager in my MainActivity with Four Fragments in it .
I have used viewpager.setOffscreenPageLimit(2) so fragments does not get recreated if am in any of two adjacent fragments but if i am in my first fragment and i go to fourth one then fragment first gets recreated .
and if save it in onSaveInstance inside fragment then that does not get called until host activity's same method is not called , which will not get called if am just swiping between fragments
Answer here also has same problem , it is saving fragments in host activity's onSaveInstance method .So i know how to save fragments state and retrieve it if my host activity gets recreated but what if only fragments gets recreated ?
So How can i save state of a fragment without depending on host activity and then retrieve it in onCreateView method of fragment ?
UPDATE
Just realized how dumb a person can be , data was there all the time i was not just refilling the views with it .
Thanks in advance.

Don't restore ViewPager when Activity is restored

The setup of my project is as follows
Activity has Fragment and it has ViewPager with pages supplied by FragmentStatePagerAdapter.
The data displayed by ViewPager is fetched from network.
When Activity is destroyed and restored, it tries to restore the Fragment that was visible in ViewPager when the Activity was destroyed. But the Fragment inside the ViewPager tries to access data structures that are not fully initialized, as a result crash happens.
I don't want the Fragment in ViewPager to be recreated.
One way that works is to pass null in super.Oncreate(savedInstance) of the Activity. But it will not allows me to restore state in any of other Fragments also which is not what I want.
What is the correct way to do it?

how to save my fragment content? how to refresh my fragment?

I have 2 fragments which are called from the action bar of an activity. Both are gridviews, the first one displays applications with a dedicated adapter, and the second one displays a file list with another adapter. My problem is that when I launch a file then when I back to my activity I switch from one fragment to another, when I come back to the previous one, its content disappears. And when I rotate tablet I have the some problem, because my Fragment restart so for this I think that removing fragment give the possibility to create a new Fragment up to date. How can I save and reload data in my fragment.
How can I manage to update the content of the first fragment while coming back from the second one ? And how to remove fragment after the rotation in order to recreate the Action with new Fragment? I asked this questions but I don't have any responses. the code is given below
If your data is just strings or integers, you can make use of shared preferences to store and retrieve data.
Solution to your first problem -how to save fragment state
Use setRetainInstance(true) in you fragments onCreate() *it prevents your fragment from destroying and hence recreating.
Add your fragment to back stack
declare your adapter globally in fragment and resuse it when you get back.
when, you get back to fragment its onCreateView() method will be called directly. hence initialize your adapter in onCreate() method and use it in onCreateView().
Solution to your second problem -how to update fragment content
For this you can use interface. create interface in your second fragment and implement it in your first fragment. prefer this doc for this,
http://www.vogella.com/tutorials/AndroidFragments/article.html#fragments_activitycommunication

Save Form fields in Fragments when swipe and getBack them in another fragment

I have A ViewPager which have three fragments attached to it. Each fragment has a form which user need to fill out. On last page(Fragment) there is an update button which send the data to server. So I need to collect the data from First and second fragment and get it back in third fragment so that I can send the data to server.
To do that I have just defined Interfaces in first and second fragment which send the data to parent FragmentActivity of-course I have implemented these Interfaces in FragmentActivity. These method save the data in FragmentActivity so that I can get this data in third fragment.
For this I need a callback on swipe so when user swipe from form-1 and form-2 I can send the data to parent FargmentActivity.
Now I don't know when to call these method so that the form-1 and form-2 data in first and second fragment get saved in parent `FragmentActivity'. I have tried all callback method in Fragment but no method get called when user swipe. Any Help would be Appreciated.
You can use a ViewPager.OnPageChangeListener - setOnPageChangeListener(ViewPager.OnPageChangeListener listener)
This will "Set a listener that will be invoked whenever the page changes or is incrementally scrolled"
You would use this in your activity to then request the current data from the current Fragment.
Since this fires on incremental scrolling, you could override FinishUpdate in your PagerAdapter, which seems to be fired when the current view has finished changing.

Categories

Resources