android fragment addtobackstack,Android Fragment Back Stack的问题

我对android片段Backstack的工作方式遇到了一个很大的问题,对于提供的任何帮助将不胜感激。

假设您有3个片段

[1] [2] [3]

我希望用户能够导航[1] > [2] > [3]但在返回的途中(按返回按钮)[3] > [1]。

就像我想象的那样,这可以通过addToBackStack(..)在创建将片段[2]带入XML定义的片段持有者的事务时不调用来实现。

现实情况似乎是,如果我不想[2]在用户按下“后退”按钮时再次出现[3],则我不能调用addToBackStack显示片段的事务[3]。这似乎完全违反直觉(也许来自iOS世界)。

无论如何,如果我这样做,当我离开[1] > [2]并按回去时,我会按[1]预期到达。

如果我走了[1] > [2] > [3]然后按回去,我跳回了[1](按预期)。现在,当我尝试[2]从再次跳到时,就会发生奇怪的行为[1]。首先在[3]显示之前简要显示[2]。如果此时我按回,[3]将显示,如果我再次按回,则该应用程序退出。

谁能帮我了解这里的事吗?

这是我的主要活动的布局xml文件:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/headerFragment"

android:layout_width="match_parent"

android:layout_height="wrap_content"

class="com.fragment_test.FragmentControls" >

android:id="@+id/detailFragment"

android:layout_width="match_parent"

android:layout_height="fill_parent"

/>

更新 这是我用来通过导航继承构建的代码

Fragment frag;

FragmentTransaction transaction;

//Create The first fragment [1], add it to the view, BUT Dont add the transaction to the backstack

frag = new Fragment1();

transaction = getSupportFragmentManager().beginTransaction();

transaction.replace(R.id.detailFragment, frag);

transaction.commit();

//Create the second [2] fragment, add it to the view and add the transaction that replaces the first fragment to the backstack

frag = new Fragment2();

transaction = getSupportFragmentManager().beginTransaction();

transaction.replace(R.id.detailFragment, frag);

transaction.addToBackStack(null);

transaction.commit();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值