简而言之,我有这样的结构:
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);
}
}
我得到illegal state exception
:executePendingTransaction 的递归入口。我知道如何解决它,但我只是好奇为什么会发生这种情况。在我看来,它是这样的:
- 在活动的 onCreate 中,我创建了片段并将其传递给寻呼机的适配器。
FragmentManager 调用 fragment 的 onCreateView
我调用了 onViewCreated 并将我的片段再次传递给寻呼机的适配器,我想它会再次进入第 2 步。