我承認我仍然片段掙扎,目前我不知道在哪裏加載的內容:找不到殘片的onCreate
起初我裝我的片段的OnCreate()我的活動,但隨後我有困難(通過findViewById)來訪問它,並把它搬到在onStart(),但我仍然不能找到在onStart()的片段內的意見...
所以的onCreate:
setContentView(R.layout.myfrag);
FrameLayout fl = (FrameLayout)findViewById(R.id.iPortraitView); // different for Landscape
if(fl.getChildCount() == 0) {
getFragmentManager().beginTransaction()
.add(R.id.iTestView, new AFragmentClass).commit();
}
else {
getFragmentManager().beginTransaction()
.replace(R.id.iTestView, new AFragmentClass).commit();
}
Log.d("myTest", String.valueOf(fl.getChildCount()));
現在的問題是,fl.getChildCount()在onCreate中總是返回0,因此增加了新的Fragments,而實際上在那裏有東西,當我在onCreate中調用相同的代碼(fl.getchildCount())時,我得到了正確的計數(每次我明顯增加I改變方向)。因此,我的景觀和肖像佈局都有重疊的視圖。
我在這裏是一個損失,並猜測我努力弄清楚什麼時候加載(特別是當我可以訪問他們)。此外,我是否必須實現潛在的潛在所有監聽器(如各種片段中包含的按鈕),以便我可以在我的片段中動態加載?
當你在你的活動中添加你的片段時,給它一個字符串'TAG',使用額外的重載,.add(R.id.iTestView,new AFragmentClass(),「AFragment」)。commit()' ,這樣以後你可以調用'getFragmentManager.findFragmentByTag(「AFragment」)',你就可以訪問片段。您也可以使用此答案中的片段ID,http://stackoverflow.com/a/6751537/418505 – Selecsosi