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