fragment笔记_针对developer doc
its own layout and
its own behavior
with its own lifecycle callbacks
封装一些彼此相关的UI逻辑
使得这部分逻辑可以复用
一个例子:
you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen spac
to continue with the news application example—the application can embed two fragments inActivity A, when running on a tablet-sized device. However, on a handset-sized screen, there's not enough room for both fragments, so?Activity A?includes only the fragment for the list of articles, and when the user selects an article, it starts?Activity B, which includes the second fragment to read the article. Thus, the application supports both tablets and handsets by reusing fragments in different combinations
时机:Once the activity reaches the resumed state, you can freely add and remove fragments to the activity
改变关联,修改fragment.container.parent
对于layout.xml类型,你可以直接设置android:id
对于非layout.xml类型,你可以通过container.id来找到fragment
Activity的状态迁移,将直接影响,fragment的状态迁移
when the activity receives?onPause(), each fragment in the activity receives?onPause().
当一个fragment attach到一个activity上
但是,android并不认为detach()之后,fragment要成为垃圾,要被destroy(也意味着:Android依然会控制该fragment的生命)
所以,只能理解,detach()仅仅是为了表达:fragment的view应该被回收(这在逻辑上,也是通的)
An activity is placed into a back stack of activities that's managed by the system when it's stopped, by default (so that the user can navigate back to it with the?Back?button, as discussed in?Tasks and Back Stack). However, a fragment is placed into a back stack managed by the host activity only when you explicitly request that the instance be saved by calling?addToBackStack()?during a transaction that removes the fragment.
猜想:一个transact被放入到backstack,那么点击back时,就让transact做一个相反的操作,如:之前做的是detach,那么back时,做的就是attac
?
通信:
● Fragment和activity间的通信
Fragment.getactivity()得到activity
Fragmentmanager.findfragmentby…()得到fragment
● Fragment之间
请使用设计模式中的 观察者模式(设置好依赖关系)
?
样式:
●?包含Ui的fragment
The?android:name?attribute in the?<fragment>?specifies the?Fragment?class to instantiate in the layout.
When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the?onCreateView()?method for each one, to retrieve each fragment's layout.
The system inserts the?Viewreturned by the fragment directly in place of the?<fragment>?element.
●?无UI的fragment
you can also use a fragment to provide a background behavior for the activity without presenting additional UI.