更换新的堆栈片段回栈 [英] Replace fragment back stack with new stack

查看:149
本文介绍了更换新的堆栈片段回栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想完全取代片段回来一个我生成基于​​通过网络连接返回一些信息叠加。我首先弹出返回堆叠我想要的位置(即正常工作...但可以说我弹到了根简单),然后我尝试建立和应用堆栈的片段是这样的:

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

当我运行它,最顶部的FolderFragment显示正常,但是当我打的后退按钮(或弹出堆栈),视图恢复到该点立即上述code运行前(即替代在我与循环创造了新的碎片堆回去,我回去的状态只是想添加/创建这个堆栈)之前。

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).

如果有帮助,我在我的项目采用了Android兼容包。

If it helps, I'm using the Android Compatibility Package in my project.

请帮忙。谢谢

推荐答案

我找到了答案。你必须为你想添加到您的堆栈每个新片段创建独特的交易。我本来以为这不会是必要的,但我想这是不是这样。所以,这里是答案:

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

这篇关于更换新的堆栈片段回栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆