第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android Fragment Back Stack的問題

Android Fragment Back Stack的問題

牧羊人nacy 2019-10-14 15:06:57
我對android片段Backstack的工作方式遇到了一個很大的問題,對于提供的任何幫助將不勝感激。假設您有3個片段[1] [2] [3]我希望用戶能夠?qū)Ш絒1] > [2] > [3]但在返回的途中(按返回按鈕)[3] > [1]。就像我想象的那樣,這可以通過addToBackStack(..)在創(chuàng)建將片段[2]帶入XML定義的片段持有者的事務時不調(diào)用來實現(xiàn)?,F(xiàn)實情況似乎是,如果我不想[2]在用戶按下“后退”按鈕時再次出現(xiàn)[3],則我不能調(diào)用addToBackStack顯示片段的事務[3]。這似乎完全違反直覺(也許來自iOS世界)。無論如何,如果我這樣做,當我離開[1] > [2]并按回去時,我會按[1]預期到達。如果我走了[1] > [2] > [3]然后按回去,我跳回了[1](按預期)?,F(xiàn)在,當我嘗試[2]從再次跳到時,就會發(fā)生奇怪的行為[1]。首先在[3]顯示之前簡要顯示[2]。如果此時我按回,[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"        />更新 這是我用來通過導航繼承構(gòu)建的代碼    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();
查看完整描述

3 回答

?
慕仙森

TA貢獻1827條經(jīng)驗 獲得超8個贊

說明:這是怎么回事?

如果我們牢記這.replace()一點.remove().add(),我們將通過文檔了解這一點:


替換添加到容器中的現(xiàn)有片段。這本質(zhì)上與調(diào)用remove(Fragment)所有當前添加的片段相同,這些片段在此處添加了相同的參數(shù)containerViewId,然后又add(int, Fragment, String)添加了相同的參數(shù)。


然后發(fā)生的事情是這樣的(我在片段中添加數(shù)字以使其更清楚):


// transaction.replace(R.id.detailFragment, frag1);

Transaction.remove(null).add(frag1)  // frag1 on view


// transaction.replace(R.id.detailFragment, frag2).addToBackStack(null);

Transaction.remove(frag1).add(frag2).addToBackStack(null)  // frag2 on view


// transaction.replace(R.id.detailFragment, frag3);

Transaction.remove(frag2).add(frag3)  // frag3 on view

(這里所有誤導性的東西開始發(fā)生)


請記住,.addToBackStack()只保存事務而不保存片段本身!所以現(xiàn)在我們有了frag3布局:


< press back button >

// System pops the back stack and find the following saved back entry to be reversed:

// [Transaction.remove(frag1).add(frag2)]

// so the system makes that transaction backward!!!

// tries to remove frag2 (is not there, so it ignores) and re-add(frag1)

// make notice that system doesn't realise that there's a frag3 and does nothing with it

// so it still there attached to view

Transaction.remove(null).add(frag1) //frag1, frag3 on view (OVERLAPPING)


// transaction.replace(R.id.detailFragment, frag2).addToBackStack(null);

Transaction.remove(frag3).add(frag2).addToBackStack(null)  //frag2 on view


< press back button >

// system makes saved transaction backward

Transaction.remove(frag2).add(frag3) //frag3 on view


< press back button >

// no more entries in BackStack

< app exits >

可能的解決方案

考慮實現(xiàn)FragmentManager.BackStackChangedListener以觀察后向堆棧中的更改,并在onBackStackChanged()methode中應用您的邏輯:


跟蹤交易計數(shù);

通過名稱檢查特定交易 FragmentTransaction.addToBackStack(String name);

等等。


查看完整回答
反對 回復 2019-10-14
?
慕尼黑8549860

TA貢獻1818條經(jīng)驗 獲得超11個贊

對?。?!經(jīng)過多拉頭發(fā),我終于弄清楚了如何使其正常工作。


似乎在按下后退按鈕時并未從視圖中刪除片段[3],因此您必須手動進行操作!


首先,不要使用replace(),而要分別使用remove和add。似乎replace()無法正常工作。


下一步是重寫onKeyDown方法,并在每次按下后退按鈕時刪除當前片段。


@Override

public boolean onKeyDown(int keyCode, KeyEvent event)

{

    if (keyCode == KeyEvent.KEYCODE_BACK)

    {

        if (getSupportFragmentManager().getBackStackEntryCount() == 0)

        {

            this.finish();

            return false;

        }

        else

        {

            getSupportFragmentManager().popBackStack();

            removeCurrentFragment();


            return false;

        }




    }


    return super.onKeyDown(keyCode, event);

}



public void removeCurrentFragment()

{

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();


    Fragment currentFrag =  getSupportFragmentManager().findFragmentById(R.id.detailFragment);



    String fragName = "NONE";


    if (currentFrag!=null)

        fragName = currentFrag.getClass().getSimpleName();



    if (currentFrag != null)

        transaction.remove(currentFrag);


    transaction.commit();


}

希望這可以幫助!


查看完整回答
反對 回復 2019-10-14
  • 3 回答
  • 0 關(guān)注
  • 472 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號