我在 「ActionBarMain.java」 創建以下片段通過標籤不ID
FragMent1 fram1;
FragmentTransaction fragMentTra = null;
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (tab.getText().equals("Collection")) {
try {
rl.removeAllViews();
} catch (Exception e) {
}
fram1 = new FragMent1();
fragMentTra.addToBackStack(null);
fragMentTra = getFragmentManager().beginTransaction().add(rl.getId(), fram1);
fragMentTra.commit();
}
//And in here I create a new activity
Intent intent = new Intent(this, AddBookActivity.class);
startActivity(intent);
刷新片段所以很自然,我應該能夠執行findFragmentByTag(fram1)
現在我在「AddBookActivity」中添加了一個新項目給fram1
打印出來的數組列表simple_list_item_1
,所以現在我想刷新fram1,我該怎麼做AddB ookActivity?
//AddBookActivity.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addBook:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment newFragment = new FragMent1();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
//How do I find out the fragment id from findFragmentByTag(fram1)????
transaction.replace(R.id.your_fragment_id, newFragment);//Whats the id, I have the Tag that is fram1
transaction.addToBackStack(null);
transaction.commit();
return true;
case R.id.cancel:
super.finish();
return (true);
}
return (super.onOptionsItemSelected(item));
}
但是,如何使用「transaction.replace(R.id.your_fragment_id,newFragment,」fram1「);」如果我不知道「your_fragment_id」是什麼? – anders
findFragmentByTag(「fram1」)返回一個片段而不是ID – anders
「your_fragment_id」是現有「片段」或佔位符的ID。你需要閱讀文檔... – techiServices