开发者

Replace fragment back stack with new stack

开发者_开发百科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


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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜