I am building an app that consists of actionbar with tabs, and each tab consists of a fragment. One of the fragments has data that need to be loaded. So I want the data to be loaded only the first time. When the user changes the tabs and goes back to the one containing that fragment, doesn't wait for the data to be loaded again. I assume I have to use some kind of saving the data so that it can be used the next time the user goes back to that particular fragment. But I just do not know what method I should use. If someone knows or can suggest an example of saving fragment state, please let me know. Thank u!
Try this:
myFragment.setRetainInstance(true);
It'll mean the fragment isn't recreated every time it's added.
Related
I am new to android development so forgive if i asked a silly question. Below i am narrating my requirement and the solution I thought of please read and help me if you have any solution.
In my application i have a activity that contain a toolbar on top, different options in the bottom and in the center area a framelayout where i will load fragments that will render different advertisements(Mostly images) based on the options the fragments using viewpager shows multiple images. Check the following image.
Now my problem is let's assume i selected Fashion option with featured sub option then title should be changed to Fashion-Featured and Fragment should show images related to Fashion>featured let's call this fragment fragment1. Now if the user tap on an image then the application will render another fragment lets call it fragment2.
In the fragment2 the selected image will be rendered in large with two buttons More and Stores. Here if the user taps on more button then the thumbnail images of fashion>featured option will be displayed so that user can select another image. If the user taps on store button then the another fragment will load let's call it fragment3.
In fragment3 the stores selling that particular product will be listed.
Now my problem is how can i pass the data between activity to fragment1 then fragment1 to fragment2 then from fragment2 to fragment3 so that the if the user press backbutton still the fragment2 should show the fashion>featured images with the tapped image on large and if the user press another backbutton then fragment1 should show fashion>featured option images. The data passing should be fail safe that is if the app goes in the background then come to foreground then also it should work.
Please help me out. I am planning to implement using sqlite database and reference in shared preference. I think this is not an exceptional requirement other application also handles this kind of requirement so i need a better way to achieve this.
If you would like to pass some data to a new fragment see this answer: How to pass a variable from Activity to Fragment, and pass it back?
If your fragment is already created you can add some method to it and call this method with required argument.
I'm trying to send data to 3 other fragments in a Tab of 4 from the first Tab which gets the data from the server, but observing the creation of the Tabbed Fragments, First and Second is Initialized, so the second doesn't get the data and the third and fourth get it. Even, when I tried to load the data from onResume in the second Fragment Is there anyway I can let tell the Second Fragment should reload itself when the Tab is clicked. Any help would be appreciated
Maybe using OnPageChangeListener would when you detect when the SecondFragment becomes active.
I would generally recommend notifying such things through the parent Activity
I have created a tabbed android application using android.support.v4.view.PagerAdapter.
There are about seven tabs in the application and I plan to add more. Each tab contains a lot of controls (TextView, Spinners, Toggle buttons, Check boxes etc.)
On the first tab there is a drop down to select a profile. The user can 'Load' or 'Save' a profile he wants.
A profile contains data for all the controls in the tabs and I need to update the UI controls in all the tabs.
I have all the data loaded from the profile but the UI controls are not getting updated.
There is a 'UpdateUI' function which calls 'set' functions (setText, setChecked etc. for individual controls after finding its view by ID).
I was informed that only three tabs (Previous, Current and Next) are kept in memory so I wrote the application such that the 'UpdateUI' function is called to set UI data only when user swipes to that particular tab (figuring out the active fragment).
Using DDMS logs I saw that the data loaded was proper but the 'setText' or 'setXXXXX' function does not update the fragment tab.
I have also checked several possible issues:
Possibility of 'OnTextChanged' or an 'OnListener' updating the data again.
Made sure 'UpdateUI' is called from UI thread.
Using notify data change and redrawing UI to make sure UI is updated.
I am a novice Java/Android programmer. Please point me in the right direction.
viewpager.setOffscreenPageLimit(size);
you can instantiate all your fragments by setting limit then you can update all widgets inside other fragments.
If my understanding of Android Fragment management is right, once the fragment becomes invisible for user (say some other fragment completely overlayed it or in your case you change tabs) Fragment goes through onPause and possible onStop lifecycle stages, this mean it's kind of hybernated and can't be changed before it get's visible. viewpager.setOffsetPageLimit(size) tells the fragment manager (or rather pageAdapter) how many fragments should be kept hybernated, and I doubt it playing with this will change anything, but let me know if it's, because otherwise the solution may be more complicated.
What I'd do is recreate a fragment every time user gets to see it and pass your profile data to it's constructor (or following better practice newInstance() static method), it will in fact save memory since keeping many fragments there may be overwhelming. Alternatively you can check what profile is chosen everytime fragment is calling it's onResume, and update your controls there.
I'm new to the Android ViewPager.
I have a ViewPager with WebViews on each page - basically slides of web pages. Some of the pages have text input. I want to save the user input of the pages into a Java object, so I can send them to the server, as well as restore them later when reopening the ViewPager.
Any idea how to go about doing this? Originally I thought I should save whenver I change pages (OnPageChangeListener), but I can't figure out how to pull up the Fragment of the last page, so I can get hold of the input fields from the WebView to save. I need the save to occur so that the ViewPager always displays the correct data, e.g. when sliding between pages, switching orientations, pause/resume app, etc.
Thanks very much in advance.
well can go like this
Catch the user input using javascript bridge,have a database to store all the user inputs.
Then use the same user inputs while sending to server, or while restoring the webpages.
OK, I think I have figured it out:
Rather than saving when flicking between pages, I'm now saving the data in the onPause() of the Fragment itself, i.e. the Fragment for each page. The Fragment's onPause() is called when:
The Fragment is about to be destroyed (when exiting the ViewPager, or when the Fragment is outside of the ViewPager's cached range)
When the app itself is paused, e.g. switching to another app, or when it's killed
When a child Activity is opened
When the orientation of the device changes, causing the parent Activity to be recreated
This seems to the most reliable way of saving a Fragment's data.
Any comments or suggestions on this?
Thanks again.
I am having an issue here that is driving me nuts. What I have is the following:
FragmentActivity1 holds the viewPager. It instantiates the FragmentAdapter and two fragments and attach them to the ViewPager.
Fragment1 has only one button. When user clicks this button, I create one Intent and invoke a FragmentActivty. This fragmentActivity contains a form, which the user fills in and press OK. When he does that, I persist the data in the DB of the app.
Fragment2 is a ListFragment, and lists all the data that was inserted previously by the user.
By the time user completes the form and presses OK, I persist the data, like I said and finish() the activity, returning to Fragment1. WHen I swype to Fragment2, the data is not there. I need then to swype to Frag1 and then back to Frag2. Only then I can see the problem.
I have tried setting listeners between activities and fragments but, still, cant make this work.
Have anyone seen? I am willing to share my small project as well, so you guys can take a look.
Thanks,
Felipe
I usually fill my ListView in onResume() in the ListFragment. When I'm done adding new stuff to the list (In a seperate FragmentActivity, just like you), the ListView automatically gets refreshed, because onResume() gets called ;)
Another approach is using onActivityResult(), then you can update list only when something new is added or something is removed