我读到如果我们需要立即创建片段,我们必须调用executePendingTransactions()
方法 on FragmentManager
。嗯,这就是我想要做的。像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.layout.fragmentContainer, new MyFragment);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
foo(); // It is called before MyFragment's onCreateView()
}
我想知道为什么foo()
方法被称为 BEFOREMyFragment
的onCreateView()
。如您所见,我正在调用executePendingTransactions()
UI 线程。我根本不会在这里搞乱线程。