Activity内嵌Fragment,当Activity recreate时Fragment被添加多次,造成相互遮盖

当Activity因屏幕旋转或内存不足被销毁并重新创建时,系统会根据之前的状态恢复布局,如果在onCreate中未区分状态地添加Fragment,会导致重复添加。系统默认保存布局状态,但可能需要额外处理成员变量等状态。解决方案是在添加Fragment时判断Activity的状态。
摘要由CSDN通过智能技术生成

问题描述:由于某些原因(如旋转屏幕,或内存不足时)造成Activity被destroy ,当再次回到该Activity时,系统会recreate 该Activity , if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. 如果在该Activity的onCreate中存在add Fragment的代码(未区分是create 还是recreate activity),则会再次add一次该Fragment,造成该Activity中会内嵌多个相同的Fragment. 所以最终原因还是由于Activity的生命周期造成的。

Recreating an Activity

There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish(). The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the system needs to free up resources and must shut down cached processes to recover memory.

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值