Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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 = newArrayList<JSONObject>(count);

//.... pop the back stack to a certain point//replace entire nav. backstackfor(inti=0; i<count; i++)
{
  //move the transaction into the loopfinalFragmentTransactiontransaction=this.getActivity().getSupportFragmentManager().beginTransaction();
  finalJSONObjectitem= crumbsOut.get(i);
  finalStringid= item.getString("id");

  FolderFragmentcurrentFolder=newFolderFragment();//fragment displays folder contentsBundleargs=newBundle();
  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();
}

Solution 2:

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.

Post a Comment for "Replace Fragment Back Stack With New Stack"