我有一个巨大的问题,与方式的机器人片段回栈似乎工作,将最感激的任何帮助提供。
假设你有3个碎片
[1] [2] [3]
I want the user to be able to navigate [1] > [2] > [3]
but on the way back (pressing back button) [3] > [1]
.
As I would have imagined this would be accomplished by not calling addToBackStack(..)
when creating the transaction that brings fragment [2]
into the fragment holder defined in XML.
实际情况似乎是,如果我不希望 [2]
在用户按下 [3]
上的返回按钮时再次出现,我就不能在显示片段 [3]
的事务中调用 addToBackStack
。这似乎完全违反直觉(也许来自 iOS 世界)。
Anyway if i do it this way, when I go from [1] > [2]
and press back I arrive back at [1]
as expected.
如果我去 [1] > [2] > [3]
,然后按回我跳回到 [1]
(如预期)。
现在,当我尝试再次从 [1]
跳到 [2]
时,奇怪的行为发生了。首先,在 [2]
进入视野之前,简要地显示 [3]
。如果我按回在这一点 [3]
显示,如果我再次按回应用程序退出。
有人能帮我理解一下这里发生了什么吗?
下面是我主要活动的布局 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/headerFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.fragment_test.FragmentControls" >
<!-- Preview: layout=@layout/details -->
</fragment>
<FrameLayout
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();
//Create third fragment, Dont add this transaction to the backstack, because we dont want to go back to [2]
frag = new Fragment3();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detailFragment, frag);
transaction.commit();
//END OF SETUP CODE-------------------------
//NOW:
//Press back once and then issue the following code:
frag = new Fragment2();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detailFragment, frag);
transaction.addToBackStack(null);
transaction.commit();
//Now press back again and you end up at fragment [3] not [1]
非常感谢