In brief i have such construction:

class AlbumPickerFragment extends PageFragment {

...

    @Override
    public void onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         mCallback.onViewCreated(this);
    }


}


public class PlaylistPickerActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        FragmentCreatedCallback callback = new FragmentCreatedCallback();
        if (savedInstanceState == null) {
            mFragments.add(PageFragment.newInstance(PageFragment.ALBUM_FRAGMENT_TYPE, callback));
            mAdapter = new PagerAdapter(getSupportFragmentManager(), mFragments);
            mPager.setAdapter(mAdapter);
        } else {
            restoring = true;
        }
    }


    Callback extends ICallback {

           public void onViewCreated(final Fragment fragment) {
                  mFragments.add(fragment);
                  mAdapter = new PagerAdapter(getSupportFragmentManager(), mFragments);
                  mPager.setAdapter(mAdapter);
           }
    }

I got illegal state exception: recursive entry to executePendingTransaction. I know how to fix it, but i am just curious why does this happen. In my opinion it's something like this:

  1. In activity's onCreate i created fragment and passed it to pager's adapter.
  2. FragmentManager calls fragment's onCreateView

  3. I called onViewCreated and passed my fragment to pager's adapter again AND i suppose there it goes to step 2 again.

有帮助吗?

解决方案

but i am just curious why does this happen.

You're not using the fragments properly. Right now you first build the ViewPager along with its adapter containing fragments. The problem is that in those fragments you have a callback which triggers a new adapter to be set on the ViewPager using the same fragments. As you already have a transaction in the process, trying to make another one in the process will not work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top