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

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

添加到后臺堆棧后如何保持片段狀態(tài)?

添加到后臺堆棧后如何保持片段狀態(tài)?

開心每一天1111 2019-11-05 16:13:24
我寫了一個虛擬活動,可以在兩個片段之間切換。當(dāng)您從FragmentA轉(zhuǎn)到FragmentB時,F(xiàn)ragmentA被添加到后臺堆棧中。但是,當(dāng)我返回FragmentA(按回去)時,會創(chuàng)建一個全新的FragmentA,并且它所處的狀態(tài)會丟失。我感覺自己對這個問題也很滿意,但是我提供了完整的代碼示例來幫助解決問題:public class FooActivity extends Activity {  @Override public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    final FragmentTransaction transaction = getFragmentManager().beginTransaction();    transaction.replace(android.R.id.content, new FragmentA());    transaction.commit();  }  public void nextFragment() {    final FragmentTransaction transaction = getFragmentManager().beginTransaction();    transaction.replace(android.R.id.content, new FragmentB());    transaction.addToBackStack(null);    transaction.commit();  }  public static class FragmentA extends Fragment {    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,        Bundle savedInstanceState) {      final View main = inflater.inflate(R.layout.main, container, false);      main.findViewById(R.id.next_fragment_button).setOnClickListener(new View.OnClickListener() {        public void onClick(View v) {          ((FooActivity) getActivity()).nextFragment();        }      });      return main;    }    @Override public void onSaveInstanceState(Bundle outState) {      super.onSaveInstanceState(outState);      // Save some state!    }  }  public static class FragmentB extends Fragment {    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,        Bundle savedInstanceState) {      return inflater.inflate(R.layout.b, container, false);    }  }}它從不調(diào)用FragmentA.onSaveInstanceState,并且在您回?fù)魰r會創(chuàng)建一個新的FragmentA。但是,如果我在FragmentA上并且鎖定了屏幕,則會調(diào)用FragmentA.onSaveInstanceState。所以很奇怪...我期望添加到后端堆棧的片段不需要重新創(chuàng)建是我錯了嗎?這是文檔所說的:而如果您在刪除片段時確實調(diào)用了addToBackStack(),則該片段將停止并在用戶向后導(dǎo)航時恢復(fù)。
查看完整描述

3 回答

?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗 獲得超9個贊

如果從后堆棧返回片段,它不會重新創(chuàng)建片段,而是會重新使用同一實例,并從onCreateView()片段生命周期開始,請參見片段生命周期。


因此,如果要存儲狀態(tài),則應(yīng)使用實例變量而不要依賴onSaveInstanceState()。


查看完整回答
反對 回復(fù) 2019-11-05
?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗 獲得超4個贊

與Apple UINavigationController和相比UIViewController,Google在Android軟件架構(gòu)方面做得不好。與Android有關(guān)的文檔Fragment并沒有太大幫助。


當(dāng)您從FragmentA輸入FragmentB時,現(xiàn)有的FragmentA實例不會被破壞。當(dāng)您在FragmentB中按Back返回到FragmentA時,我們不會創(chuàng)建新的FragmentA實例?,F(xiàn)有的FragmentA實例onCreateView()將被調(diào)用。


關(guān)鍵是我們不應(yīng)該在FragmentA的視圖中再次擴(kuò)大視圖onCreateView(),因為我們使用的是現(xiàn)有FragmentA的實例。我們需要保存并重用rootView。


以下代碼運(yùn)行良好。它不僅保持片段狀態(tài),還減少了RAM和CPU負(fù)載(因為我們僅在必要時膨脹布局)。我不能相信谷歌的示例代碼和文檔別說但總是夸大布局。


版本1(請勿使用版本1。請使用版本2)


public class FragmentA extends Fragment {

    View _rootView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

            Bundle savedInstanceState) {

        if (_rootView == null) {

            // Inflate the layout for this fragment

            _rootView = inflater.inflate(R.layout.fragment_a, container, false);

            // Find and setup subviews

            _listView = (ListView)_rootView.findViewById(R.id.listView);

            ...

        } else {

            // Do not inflate the layout again.

            // The returned View of onCreateView will be added into the fragment.

            // However it is not allowed to be added twice even if the parent is same.

            // So we must remove _rootView from the existing parent view group

            // (it will be added back).

            ((ViewGroup)_rootView.getParent()).removeView(_rootView);

        }

        return _rootView;

    }

}


正如評論所提到的,有時在中_rootView.getParent()為null onCreateView會導(dǎo)致崩潰。第2版按照dell116建議刪除了onDestroyView()中的_rootView。已在Android 4.0.3、4.4.4、5.1.0上測試。


版本2


public class FragmentA extends Fragment {

    View _rootView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

            Bundle savedInstanceState) {

        if (_rootView == null) {

            // Inflate the layout for this fragment

            _rootView = inflater.inflate(R.layout.fragment_a, container, false);

            // Find and setup subviews

            _listView = (ListView)_rootView.findViewById(R.id.listView);

            ...

        } else {

            // Do not inflate the layout again.

            // The returned View of onCreateView will be added into the fragment.

            // However it is not allowed to be added twice even if the parent is same.

            // So we must remove _rootView from the existing parent view group

            // in onDestroyView() (it will be added back).

        }

        return _rootView;

    }


    @Override

    public void onDestroyView() {

        if (_rootView.getParent() != null) {

            ((ViewGroup)_rootView.getParent()).removeView(_rootView);

        }

        super.onDestroyView();

    }

}

警告!??!


這是哈克!雖然我在我的應(yīng)用程序中使用它,但是您需要仔細(xì)測試和閱讀評論。


查看完整回答
反對 回復(fù) 2019-11-05
?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗 獲得超4個贊

我猜想有另一種方法可以實現(xiàn)您想要的。我沒有說這是一個完整的解決方案,但在我的情況下達(dá)到了目的。


我所做的是代替了我剛剛添加目標(biāo)片段的片段。因此,基本上,您將改為使用add()method replace()。


我還做了什么。我隱藏了當(dāng)前的片段,并將其添加到堆棧中。


因此,它在不破壞其視圖的情況下將新片段覆蓋在當(dāng)前片段上。(檢查onDestroyView()是否未調(diào)用其方法。加上它可以backstate使我恢復(fù)片段。


這是代碼:


Fragment fragment=new DestinationFragment();

FragmentManager fragmentManager = getFragmentManager();

android.app.FragmentTransaction ft=fragmentManager.beginTransaction();

ft.add(R.id.content_frame, fragment);

ft.hide(SourceFragment.this);

ft.addToBackStack(SourceFragment.class.getName());

ft.commit();

AFAIK系統(tǒng)僅onCreateView()在視圖被破壞或未創(chuàng)建時調(diào)用。但是這里我們通過不從內(nèi)存中刪除視圖來保存視圖。因此,它不會創(chuàng)建新視圖。


當(dāng)您從Destination Fragment返回時,它將彈出最后一個FragmentTransaction刪除的頂部片段,這將使最上面的(SourceFragment's)視圖顯示在屏幕上。


評論:正如我所說,這不是一個完整的解決方案,因為它不會刪除Source片段的視圖,因此比平時占用更多的內(nèi)存。但還是要達(dá)到目的。另外,我們使用完全不同的隱藏視圖機(jī)制,而不是替換非傳統(tǒng)的視圖。


因此,這實際上與維護(hù)狀態(tài)無關(guān),而與維護(hù)視圖有關(guān)。


查看完整回答
反對 回復(fù) 2019-11-05
  • 3 回答
  • 0 關(guān)注
  • 504 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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