我使用BackStack存储片段,它工作正常。但是,当我想从BackStack中删除片段时,没有任何反应。 我检查了一些像this这样的问题,看到他们确实通过像我一样使用popBackStack从BackStack中删除了一个项目,但它对我不起作用。
这是我的代码:
String previousTag = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1).getName();
Fragment previousInstance = getSupportFragmentManager().findFragmentByTag(previousTag);
fragmentTransaction.replace(R.id.container_body, previousInstance,previousTag);
fragmentTransaction.commit();
currentInstance正确显示最顶层的片段。有趣的是count和count1是等于,并且使用popBackStack命令后堆栈中最顶层的项目仍然存在。所以我无法从堆栈中删除最顶层的片段。
如果使用此代码:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<script>
function IsValidTest(Control) {
name = Control.name;
var list = document.getElementById("TxtEmpcode");
var contents = $("[id=TxtEmpcode]");
var TextboxId = name;
var Txtbox = document.getElementsByName(name);
var TextboxValue = Txtbox[0].value;
for (i = 0; i < contents.length; i++)
{
var currenttextboxName=contents[i].name;
var currenttextboxValue=contents[i].value;
//Compare currently changed value with existing values in other textboxes
if (name != currenttextboxName && currenttextboxValue == TextboxValue)
{
alert('Employee Code has already been entered');
Txtbox[0].value = '';
return false;
}
}
return true;
}
</script>
<div>
<asp:GridView ID="GridView2" runat="server" Style="margin-left: 23px; margin-top: 11px;"
Width="420px" CellPadding="4" AutoGenerateColumns="False" ForeColor="#333333"
GridLines="None" Height="213px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Employee Code">
<ItemTemplate>
<asp:TextBox ID="TxtEmpcode" runat="server" AutoPostBack="true" ClientIDMode="Static" onchange="javascript: return IsValidTest(this);"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Content>
我可以用前一个替换片段,但这不是正确的方法,因为我无法回复多个片段。
答案 0 :(得分:3)
我发现了:popBackStack方法在调用后不会立即弹出。所以在我必须使用popBackStackImmediate方法而不是popBackStack。 这是我最后更正的代码:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (getSupportFragmentManager().getBackStackEntryCount()> 0) {
fragmentManager.popBackStackImmediate(getSupportFragmentManager().getBackStackEntryCount() - 1, FragmentManager.POP_BACK_STACK_INCLUSIVE);
String previousTag = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1).getName();
Fragment previousInstance = getSupportFragmentManager().findFragmentByTag(previousTag);
fragmentTransaction.replace(R.id.container_body, previousInstance, previousTag);
fragmentTransaction.commit();
}