I create the following fragment in “ActionBarMain.java”
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);
So naturally I should be able to perform the findFragmentByTag(fram1)
Now I am in the “AddBookActivity” and have added a new item to an arraylist that fram1 prints out in a simple_list_item_1 so now I want to refresh fram1, how do I do that in AddBookActivity??
//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));
}
findFragmentByTagtakes aStringas a parameter, not aFragmentso you would use
to add, and replace using
to find the fragment, use
Whether that will update your list is another matter, since the code you posted does not address that.
EDIT:
Your
Activitycan load multipleFragments, and therefore theActivitylayout will depend on the screen size. Simplest use is that it is only large enough for oneFragment. ThisFragmentis aFragmentListor it contains aListView. The list requires anAdapterto bind the data to the list. Simple example is aArrayAdapterof typeStringe.g.ArrayAdapter<String>. You assign theListViewto use theArrayAdapteras its data source. You can then change theArrayAdapters underlaying data (ArrayList<String>orString[]) directly (requires the adapter to be notified of the change) or via the adapter itself. More complex views can be achieved by extending a adapter class.I suggest looking at
ListViewandFragmentListSDK samples.