<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Fragment on Website of SauceWu</title><link>https://saucewu.github.io/tags/fragment/</link><description>Recent content in Fragment on Website of SauceWu</description><generator>Hugo -- gohugo.io</generator><language>zh</language><lastBuildDate>Sat, 10 Feb 2018 15:00:00 +0000</lastBuildDate><atom:link href="https://saucewu.github.io/tags/fragment/index.xml" rel="self" type="application/rss+xml"/><item><title>Fragment 踩坑实录</title><link>https://saucewu.github.io/posts/fragment-%E8%B8%A9%E5%9D%91%E5%AE%9E%E5%BD%95/</link><pubDate>Sat, 10 Feb 2018 15:00:00 +0000</pubDate><guid>https://saucewu.github.io/posts/fragment-%E8%B8%A9%E5%9D%91%E5%AE%9E%E5%BD%95/</guid><description>&lt;h1 id="fragment-踩坑实录"&gt;Fragment 踩坑实录&lt;/h1&gt;
&lt;h3 id="不要轻易使用commitnowallowingstateloss"&gt;不要轻易使用commitNowAllowingStateLoss&lt;/h3&gt;
&lt;p&gt;在很多情况下使用不正确使用Fragment 会导致 java.lang.IllegalStateException 网上很多推荐commitNowAllowingStateLoss 来治标
但使用commitNowAllowingStateLoss 会导致当前Fragment 以其子Fragment 丢失状态 如 getVisibility() isHidden() 等等 而且会出现很多不可预知的问题&lt;/p&gt;
&lt;p&gt;出现问题:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;1 在不正确的时机commit 如 onActivityResult 回调方法中
fragmentManager的 commit方法只能在onResume-onPause 的状态中使用
如必须在其他状态下改变状态 建议将状态记录下来 在onResume中执行操作&lt;/li&gt;
&lt;li&gt;2 重复add 同一个Fragment
由于commit属于异步操作 可能由于commit 过快导致 判断fragment是否为空的方法失效
应先使用executePendingTransactions让上一次commit执行完成后再执行新的操作&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="不要在初始化方法中直接生成新的fragment"&gt;不要在初始化方法中直接生成新的Fragment&lt;/h3&gt;
&lt;p&gt;在很多Activity 被重建的情况下 直接生成新的Fragment 会导致旧Fragment 无法回收导致泄露 如果在这些fragment中还有轮训或者socket 回调会导致崩溃
解决方案:
为每个Fragment添加tag
使用findFragmentByTag() 获取Fragment 若为null 再创建新的Fragment&lt;/p&gt;</description></item></channel></rss>