1

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

apaderno
  • 28,547
  • 16
  • 75
  • 90
RyanM
  • 5,680
  • 9
  • 45
  • 55

2 Answers2

5

I found the answer. You have to create unique transactions for each new fragment you want to add to your stack. I originally thought this wouldn't be necessary, but I guess this is not so. So, here is the answer:

ArrayList<JSONObject> crumbsOut = new ArrayList<JSONObject>(count);

//.... pop the back stack to a certain point

//replace entire nav. backstack

for(int i = 0; i<count; i++)
{
  //move the transaction into the loop
  final FragmentTransaction transaction = this.getActivity().getSupportFragmentManager().beginTransaction();
  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
  //move the commit into the loop
  transaction.commit();
}
RyanM
  • 5,680
  • 9
  • 45
  • 55
0

could it be that you are doing everything in the same method and your beginTransaction() call is cancelling the pop (the FragmentManager no doubt begins a transaction to do the pop).-

I would suggest doing the clean up yourself using the same FragmentTransaction and only performing a single commit. Alternatively you could post your replacement calls into the main threads message queue so that it is performed 'later,.

As you're using the compat library you can always debug the source to see what's going on.

PJL
  • 18,735
  • 17
  • 71
  • 68
  • Humm... even if I comment out the code for popping the stack, the result is the same; only the last fragment in the loop is added to the stack and going back returns me to a state right before the above code is committed. Also, what do you mean by "I would suggest doing the clean up yourself using the same FragmentTransaction...?" Thanks. – RyanM Jun 07 '11 at 13:31