Android 中Fragment的自动重建

本文介绍了Android中Fragment的两种重建方式:通过setRetainInstance方法控制Fragment在Activity重建时保留实例,以及FragmentActivity如何自动重建管理的Fragment。详细解析了setRetainInstance的工作原理以及FragmentActivity在配置改变时Fragment的保存和重建过程。
摘要由CSDN通过智能技术生成

Fragment常见的两种重建方式,一种是通过调用setRetainInstance来通知系统在重建Activity(例如屏幕配置改变)时保留此Fragment;另一种方式是系统在重建Activity时自动重建Fragment,典型例子是FragmentActivity对管理的Fragment的重建。

setRetainInstance

直接参考官方文档的解释:

Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back stack. If set, the fragment lifecycle will be slightly different when an activity is recreated:

  • onDestroy() will not be called (but onDetach() still will be, because the fragment is being detached from its current activity).
  • onCreate(android.os.Bundle) will not be called since the fragment is not being
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值