我的主要应用程序有一个blanck FrameLayout,原因是我可以随时随地添加和替换片段......
到目前为止,我没有遇到任何问题。我已成功添加第一个片段并且onCreate被调用等,然后在我的代码中使用一个按钮我用我的第二个片段替换FrameLayout似乎执行,但是没有显示?因为onActivityCreated()不会被调用。
我的主应用程序调用FrameLayout xml文件,称为send_activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
sendControlFragment sendControlFragment = new sendControlFragment();
peerItemFragment fragmentList = new peerItemFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_activity);
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
sendControlFragment controlFragment = new sendControlFragment();
// Add the fragment to the 'fragment_container' FrameLayout
fragmentTransaction.add(R.id.fragment_container, controlFragment).commit();
}
}
以上作品完美无缺......
我调用第二个片段的按钮,我被告知要启动我已经完成的新FragmentTransaction,但是这不会调用onActivityCreated()并跳转到fragmentList.addPeers(mService.peerList); < /强>
这很好,它调用addPeers()方法,但没有onActivityCreated()然后崩溃!
public void authenticateClick(View v) {
mService.peerSearch();
peerItemFragment peerFragment = new peerItemFragment();
FragmentTransaction fragmentTransactionauthen = fragmentManager.beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
fragmentTransactionauthen.replace(R.id.fragment_container, peerFragment);
fragmentTransactionauthen.addToBackStack(null);
// Commit the transaction
fragmentTransactionauthen.commit();
//pass peer list to fragment to display
fragmentList.addPeers(mService.peerList);
}
根据我的知识,你能否建议解决方案为.replace并不会调用onActivityCreated()方法......
答案 0 :(得分:0)
因为我使用的是listfragment,所以我必须将listView id替换为以下内容,因为这会导致致命的错误!
android:id="@android:id/list"
它现在可以使用并替换碎片。