android-Fragment

来源:互联网 发布:晚会暖场小游戏知乎 编辑:程序博客网 时间:2024/06/04 23:01

Fragment的类型有:DialogFragment;ListFragment;PreferenceFragment

不保存当前的Fragment时,使用Fragment.commitAllowingStateLoss()方法

For example, if a news application has two fragments in an activity—one to show a list of articles (fragment A) and another to display an article (fragment B)—then fragment A must tell the activity when a list item is selected so that it can tell fragment B to display the article. In this case, the OnArticleSelectedListener interface is declared inside fragment A:

Fragment A通过Activity传输数据到Fragment B,实现机制:Fragment A定义一个interface,Activity去实现接口,并将返回通知给Fragment B。

将Activity的数据传给Fragment时,可以使用Bundle.

Also like an activity, you can retain the state of a fragment using a Bundle, in case the activity's process is killed and you need to restore the fragment state when the activity is recreated. You can save the state during the fragment'sonSaveInstanceState() callback and restore it during either onCreate()onCreateView(), oronActivityCreated()

横竖屏切换时,Activity状态的保存。

0 0
原创粉丝点击