Android Fragment小記

來源:互聯網
上載者:User

標籤:android   for   cti   on   c   ad   r   as   tt   

1、remove,add,replace

remove:移除fragment,被移除的fragment會銷毀掉,包括執行個體和介面。

add:

add(int containerViewId, Fragment fragment)
Calls add(int, Fragment, String) with a null tag.

add(Fragment fragment, String tag)
Calls add(int, Fragment, String) with a 0 containerViewId.

add(int containerViewId, Fragment fragment, String tag)
Add a fragment to the activity state.

replace:等同於remove+add。

說明:往同一個containerViewId中replace會移除掉舊的fragment,包括執行個體和介面,然後add新的fragment,只會有一個fragment存在。

add會往同一個containerViewId中添加多個fragment,包括執行個體和介面,後加進去的會疊加在前面的fragment的上面,可以有多個fragment存在。

2、backstack fragment的後退棧

在調用add或者replace方法之後,調用commit方法之前加上addToBackStack會將當前的fragment加入到backstack。

說明:在調用replace方法前,如果某個fragment已加入到後退棧,然後這個時候調用replace被別的fragment替換了,此fragment也就銷毀了,但是

還是可以按返回鍵從後退棧中彈出,其實也就是說fragment和後退棧中的fragment不是一回事。

3、清空backstack方法

popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)。

public static final int POP_BACK_STACK_INCLUSIVE

Added in API level 11
Flag for popBackStack(String, int) and popBackStack(int, int): If set, and the name or ID of a back stack entry has been supplied, then all matching entries will be consumed until one that doesn‘t match is found or the bottom of the stack is reached. Otherwise, all entries up to but not including that entry will be removed.

Android Fragment小記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.