Rya*_*anM 1 java android android-fragments
我正在尝试使用基于通过网络连接返回的一些信息生成的片段来完全替换片段后备栈.我首先将后端堆栈弹出到我想要的位置(工作正常......但是为了简单起见,我会说我弹出到根目录),然后我尝试构建并应用这样的片段堆栈:
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();
Run Code Online (Sandbox Code Playgroud)
当我运行它时,最顶层的FolderFragment正确显示,但当我点击后退按钮(或弹出堆栈)时,视图将恢复到上述代码运行之前的那一刻(即不是回到堆栈中我用循环创建的新片段,我在尝试添加/创建此堆栈之前返回到状态.
如果它有帮助,我在我的项目中使用Android兼容包.
请帮忙.谢谢
我找到了答案.您必须为要添加到堆栈的每个新片段创建唯一的事务.我原本以为这不是必要的,但我想这并非如此.所以,这是答案:
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4581 次 |
| 最近记录: |