0

I have a ViewPager coupled with a FragmentStatePager that has around 8 Fragments attached to it. The application is designed as a wizard. The user enters information on one page swpies to the next page and so on. Now i am having problem collecting the information and restoring the information. When a user swipes forward how can i save my information and similarly when i swpie back what functions always gets called so that i can read the data from my Object and populate the Screen UI ? I have tried the OnSaveInstanceState but that is hardly ever called. OnResume is sometimes called sometimes not. Also note i am using RoboGuice to inject views into my fragment so the ui fields are only available after the OnViewCreated function. Also note each of my eight fragments implement the following interface. In both these functions a application level singelton is either wriiten to or read from and access the UI fields in the Fragment. I just cannot find out where to plug these functions into ? Any suggestions would be of great use. I would have gussed the setOnPageChangeListener but the onPageSelected event is called before the actual page is visible to the user and can cause a null pointer exception as my RoboGuice injections are not really ready.

public interface FragmentState {
    public void saveFragementState();
    public void restoreFragementState();
}
4

1 回答 1

0

您应该使用ViewPager.setOffscreenPageLimit(k)缓存从 nk 到 n+k 的 ViewPager 的所有片段。

然后就可以onPageSelected()安全使用了:

    pager.setOnPageChangeListener(new OnPageChangeListener() {
        public void onPageScrollStateChanged(int state) {

        }

        public void onPageScrolled(int position, float positionOffset,
                int positionOffsetPixels) {
        }

        public void onPageSelected(int position) {
            fragments.get(position).doThings();
        }
    });
于 2013-12-31T21:33:47.937 回答