I'm trying to completely replace a fragment back stack with one I generate based on some information returned via a network connection. I first pop the back stack to the position I want (that works fine... but lets say I pop to the root for simplicity), and then I try to build and apply a fragment stack like this:
ArrayList<JSONObject> crumbsOut = new ArrayList<JSONObject>(count);
//.... pop the back stack to a certain point
//replace entire nav. backstack
final FragmentTransaction transaction = this.getActivity().getSupportFragmentManager().beginTransaction();
for(int i = 0; i<count; i++)
{
final JSONObject item = crumbsOut.get(i);
final String id = item.getString("id");
FolderFragment currentFolder = new FolderFragment();//fragment displays folder contents
Bundle args = new Bundle();
args.putString(DATA_ITEM_ID_KEY, id);
args.putString(DATA_ITEM_NAME_KEY, item.getString("displayname"));
currentFolder.setArguments(args);
transaction.replace(R.id.MasterContainer, currentFolder);
transaction.addToBackStack(id);
}
// Commit the transaction
transaction.commit();
When I run this, the top-most FolderFragment is displayed properly, but when I hit the back button (or pop the stack), the view reverts to the point immediately before the above code is run (i.e. instead of going back in the stack of new fragments I created with the loop, I go back to the state just before trying to add/create this stack).
If it helps, I'm using the Android Compatibility Package in my project.
Please help. Thanks