Question

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:

  1. Possibility of 'OnTextChanged' or an 'OnListener' updating the data again.
  2. Made sure 'UpdateUI' is called from UI thread.
  3. 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.

Was it helpful?

Solution

viewpager.setOffscreenPageLimit(size);

you can instantiate all your fragments by setting limit then you can update all widgets inside other fragments.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top