我有一個工作模型的片段,當我調試代碼時,我看到片段onCreate被調用4次。片段:onCreate被調用4次
下面是我的代碼: MyFragmentActivity
class MyFragmentActivity extends FragmentActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(fragmentID, new MyListFragmentt())
.replace(detailFragmentID, new MyDetailFragment()).commit();
}
}
@Override
protected void onRestart() {
getSupportFragmentManager().beginTransaction().replace(detailFragmentID, new MyDetailFragment()).commitAllowingStateLoss();
}
}
MyDetailFragment.class
class MyDetailFragment extends Fragment{
// has method like oncreate(),onCreateView(),onSaveInstanceState()
}
如何我MyDetailFragment的OnCreate中被稱爲?當我去一些其他活動,然後回來,然後傾斜設備只有oncreate和MyDetailFragment onSaveInstanceState被稱爲多次。
我如何解決這個問題,我已經看過幾篇文章,但它說我們需要使用HIDE,Show
方法和其他東西?但是對此有什麼適當的注意?
編輯
當我來了距上一次活動回來,我在MyDetailFragment數據需要被刷新。
好像烏爾創造同類片段中的多個實例。你能告訴我如何在活動中添加片段 –
@PulkitSethi對不起,我沒有得到你? – Goofy
在你的活動課你如何添加這些片段,你可以分享你的活動代碼 –