I had the same trouble and "fixed it" by a workaround: I check the
fragments are loaded and only store those on the Bundle. From time to
time it fires an Exception but it works anyway.

    @Override
    protected void onSaveInstanceState(Bundle instanceStateToSave) {
        super.onSaveInstanceState(instanceStateToSave);

        FragmentManager manager = getSupportFragmentManager();
        
        int fragmentsCount = mPagerAdapter.getCount();
        
        // page on display
        int visiblePage = mViewPager.getCurrentItem();

        instanceStateToSave.putInt("visiblePage", visiblePage);

                try {
                        manager.putFragment(instanceStateToSave,
PageFragment.class.getName() + visiblePage, (PageFragment)
mPagerAdapter.getItem(visiblePage));
                } catch (Exception e) {
// TODO something
                }

        
                // preloaded pages
                int offScreenLimit =
getResources().getInteger(R.integer.offScreenLimit); // this is a
setting I use with setOffscreenPageLimit(), usually its value is 2

                int minPage = ((visiblePage - offScreenLimit)>0)?(visiblePage -
offScreenLimit):0;
                for(int i = minPage; i < visiblePage; i++) {
                        try {
                                manager.putFragment(instanceStateToSave,
PageFragment.class.getName() + i, (PageFragment)
mPagerAdapter.getItem(i));
                        } catch (Exception e) {
// TODO something                       }
                }

                
                int maxPage = ((visiblePage + offScreenLimit +
1)>fragmentsCount)?fragmentsCount:(visiblePage + offScreenLimit + 1);
                for(int i = visiblePage + 1; i < maxPage; i++) {
                        try {
                                manager.putFragment(instanceStateToSave,
PageFragment.class.getName() + i, (PageFragment)
mPagerAdapter.getItem(i));
                        } catch (Exception e) {
// TODO something                       }
                }
        
}       



Marina

On Mon, Aug 13, 2012 at 11:15 AM, Mor G. <gaz...@gmail.com> wrote:
> There's a solution in stackoverflow:
> http://stackoverflow.com/questions/11296411/fragmentstatepageradapter-illegalstateexception-myfragment-is-not-currently
>
> but it doesn't seem to be helping me, since I'm not instantiating my
> fragments as he did, but maybe it'll help you.
>
> I'm still looking for a solution for myself.
>
> On Thursday, June 14, 2012 10:34:05 AM UTC+3, Alexandros wrote:
>>
>> Someone please?
>>
>> On Sunday, June 3, 2012 10:50:33 AM UTC+3, Alexandros wrote:
>>>
>>> Hello everyone,
>>>
>>> Some time ago, I updated my application to use the support v4 library in
>>> order to use the ViewPager control. The initial version of the application
>>> used the FragmentPagerAdapter, however, because the application has a lot of
>>> pages it seemed wise to use the FragmentStatePagerAdapter, since I was
>>> getting OutOfMemory reports. But now, I am getting reports of another error.
>>> The error is:  java.lang.IllegalStateException: Fragment <MyFragment> is not
>>> currently in the FragmentManager.
>>> I did some searching on the Internet, but nothing solid came up. The
>>> problem seems to be happening when the activity is trying to pause and the
>>> onSaveInstanceState method is called. After I did some investigation on the
>>> support v4 library my self, I noticed that this exception is thrown in the
>>> FragmentManager.putFragment() when the member of the fragment mIndex is less
>>> than 0, but I cannot figure out when mIndex becomes less than 0 and why this
>>> is not handled correctly.
>>>
>>> I would like to point out that I do not have an MOR (method of
>>> reproduction) for this error.
>>>
>>> Here is the call stack:
>>> java.lang.RuntimeException: Unable to pause activity  <my activity with
>>> the ViewPager> java.lang.IllegalStateException: Fragment <MyFragment> is not
>>> currently in the FragmentManager
>>> at
>>> android.app.ActivityThread.performPauseActivity(ActivityThread.java:3438)
>>> at
>>> android.app.ActivityThread.performPauseActivity(ActivityThread.java:3395)
>>> at
>>> android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3378)
>>> at android.app.ActivityThread.access$2700(ActivityThread.java:129)
>>> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2124)
>>> at android.os.Handler.dispatchMessage(Handler.java:99)
>>> at android.os.Looper.loop(Looper.java:143)
>>> at android.app.ActivityThread.main(ActivityThread.java:4717)
>>> at java.lang.reflect.Method.invokeNative(Native Method)
>>> at java.lang.reflect.Method.invoke(Method.java:521)
>>> at
>>> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
>>> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
>>> at dalvik.system.NativeStart.main(Native Method)
>>> Caused by: java.lang.IllegalStateException: Fragment <MyFragment> is not
>>> currently in the FragmentManager
>>> at
>>> android.support.v4.app.FragmentManagerImpl.putFragment(FragmentManager.java:516)
>>> at
>>> android.support.v4.app.FragmentStatePagerAdapter.saveState(FragmentStatePagerAdapter.java:185)
>>> at
>>> android.support.v4.view.ViewPager.onSaveInstanceState(ViewPager.java:881)
>>> at android.view.View.dispatchSaveInstanceState(View.java:6098)
>>> at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1323)
>>> at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1327)
>>> at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1327)
>>> at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1327)
>>> at android.view.View.saveHierarchyState(View.java:6081)
>>> at
>>> com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1573)
>>> at android.app.Activity.onSaveInstanceState(Activity.java:1094)
>>> at
>>> android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:480)
>>>  at <my activity with the
>>> ViewPager>.onSaveInstanceState(ContentActivity.java:288)
>>> at android.app.Activity.performSaveInstanceState(Activity.java:1044)
>>> at
>>> android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
>>> at
>>> android.app.ActivityThread.performPauseActivity(ActivityThread.java:3420)
>>>
>>> Thanks in advanced.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to