{"id":42328,"date":"2023-12-11T14:56:13","date_gmt":"2023-12-11T06:56:13","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=42328"},"modified":"2023-12-11T14:56:13","modified_gmt":"2023-12-11T06:56:13","slug":"fragment%e5%90%84%e7%a7%8d%e6%83%85%e5%86%b5%e4%b8%8b%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/fragment%e5%90%84%e7%a7%8d%e6%83%85%e5%86%b5%e4%b8%8b%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%ef%bc%9f\/","title":{"rendered":"fragment\u5404\u79cd\u60c5\u51b5\u4e0b\u7684\u751f\u547d\u5468\u671f\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                     \u7531\u4e8eFragment\u7684\u751f\u547d\u5468\u671f\u4e0eActivity\u7684\u751f\u547d\u5468\u671f\u6709\u7740\u7275\u626f\uff0c\u6240\u4ee5\u628a\u4e24\u8005\u7684\u56fe\u653e\u5230\u4e00\u8d77\u4f5c\u4e3a\u5bf9\u6bd4\u7406\u89e3\u3002<\/p>\n<p>&lt;img referrerpolicy=&quot;&quot;no-referrer&quot;&quot; src=&quot;&quot;http:\/\/res.mianshigee.com\/upload\/article\/20200111\/1354170682_3824.png&quot;&quot;&gt;<\/p>\n<p>\u00a0<\/p>\n<p>\u63a5\u4e0b\u6765\u5c31\u4e0d\u540c\u60c5\u51b5\u4e0b\u7684Fragment\u751f\u547d\u5468\u671f\u505a\u4e00\u7b80\u5355\u4ecb\u7ecd\uff1a<\/p>\n<p>Fragment\u5728Activity\u4e2dreplace<br \/>\n\u65b0\u66ff\u6362\u7684Activity\uff1aonAttach() &#8212;&gt; onCreate() &#8212;&gt; onCreatView() &#8212;&gt; onViewCreated &#8212;&gt; onActivityCreated() &#8212;&gt; onStart &#8212;&gt;onResume()<\/p>\n<p>\u88ab\u66ff\u6362\u7684Activity\uff1aonPause() &#8212;&gt; onStop() &#8212;&gt; onDestoryView() &#8212;&gt; onDestory() &#8212;&gt; onDetach()<\/p>\n<p>Fragment\u5728Activity\u4e2dreplace\uff0c\u5e76addToBackStack<br \/>\n\u65b0\u66ff\u6362\u7684Fragment\uff08\u6ca1\u6709\u5728BackStack\u4e2d\uff09\uff1aonAttach &gt; onCreate &gt; onCreateView &gt; onViewCreated &gt; onActivityCreated &gt; onStart &gt; onResume<\/p>\n<p>\u65b0\u66ff\u6362\u7684Fragment\uff08\u5df2\u7ecf\u5728BackStack\u4e2d\uff09\uff1aonCreateView &gt; onViewCreated &gt; onActivityCreated &gt; onStart &gt; onResume<\/p>\n<p>\u88ab\u66ff\u6362\u7684Fragment\uff1aonPause &gt; onStop &gt; onDestroyView<\/p>\n<p>Fragment\u5728ViewPager\u4e2d\u5207\u6362<br \/>\n\u6211\u4eec\u79f0\u5207\u6362\u524d\u7684\u7684Fragment\u79f0\u4e3aPreviousFragment\uff0c\u7b80\u79f0PF\uff1b\u5207\u6362\u540e\u7684Fragment\u79f0\u4e3aNextFragment\uff0c\u7b80\u79f0NF\uff1b\u5176\u4ed6Fragment\u79f0\u4e3aOtherFragment\uff0c\u7b80\u79f0OF\u3002<\/p>\n<p>\uff08\u5728ViewPager\u4e2dsetUserVisibleHint\u80fd\u53cd\u6620\u51faFragment\u662f\u5426\u88ab\u5207\u6362\u5230\u540e\u53f0\u6216\u524d\u53f0\uff0c\u6240\u4ee5\u5728\u8fd9\u91cc\u4e5f\u5f53\u4f5c\u751f\u547d\u5468\u671f\uff09<\/p>\n<p>\u5982\u679c\u76f8\u5173\u7684Fragment\u6ca1\u6709\u88ab\u52a0\u8f7d\u8fc7\uff1a<br \/>\nNF\uff1a setUserVisibleHint(false)\u3010\u7528\u6237\u4e0d\u53ef\u89c1\u3011 &gt; onAttach &gt; onCreate &gt; setUserVisibleHint(true)\u3010\u7528\u6237\u53ef\u89c1\u3011 &gt; onCreateView &gt; onViewCreated &gt; onActivityCreated &gt; onStart &gt; onResume<\/p>\n<p>OF\u8ddfNF\u76f8\u90bb\uff1a setUserVisibleHint(false) &gt; onAttach &gt; onCreate &gt; onCreateView &gt; onViewCreated &gt; onActivityCreated &gt; onStart &gt; onResume<\/p>\n<p>\u5982\u679c\u76f8\u5173\u7684Fragment\u5df2\u7ecf\u88ab\u52a0\u8f7d\u8fc7\uff1a<br \/>\nNF\u8ddfPF\u76f8\u90bb  \uff1asetUserVisibleHint(true)<\/p>\n<p>NF\u8ddfPF\u4e0d\u76f8\u90bb\uff1asetUserVisibleHint(true) &gt; onCreateView &gt; onViewCreated &gt; onActivityCreated &gt; onStart &gt; onResume<\/p>\n<p>PF\u8ddfNF\u76f8\u90bb  \uff1asetUserVisibleHint(false)<\/p>\n<p>PF\u8ddfNF\u4e0d\u76f8\u90bb\uff1asetUserVisibleHint(false) &gt; onPause &gt; onStop &gt; onDestroyView<\/p>\n<p>OF\u8ddfPF\u76f8\u90bb\uff1aonPause &gt; onStop &gt; onDestroyView<\/p>\n<p>OF\u8ddfNF\u76f8\u90bb\uff1aonCreateView &gt; onViewCreated &gt; onActivityCreated &gt; onStart &gt; onResume<\/p>\n<p>OF\u5939\u5728PF\u548cNF\u4e2d\u95f4\uff1a\u4e0d\u8c03\u7528\u4efb\u4f55\u751f\u547d\u5468\u671f\u65b9\u6cd5<\/p>\n<p>NF\u8ddfPF\u76f8\u90bb  \uff1asetUserVisibleHint(true)<\/p>\n<p>NF\u8ddfPF\u4e0d\u76f8\u90bb\uff1asetUserVisibleHint(true) &gt; onCreateView &gt; onViewCreated &gt; onActivityCreated &gt; onStart &gt; onResume<\/p>\n<p>PF\u8ddfNF\u76f8\u90bb  \uff1asetUserVisibleHint(false)<\/p>\n<p>PF\u8ddfNF\u4e0d\u76f8\u90bb\uff1asetUserVisibleHint(false) &gt; onPause &gt; onStop &gt; onDestroyView<\/p>\n<p>OF\u8ddfPF\u76f8\u90bb\uff1aonPause &gt; onStop &gt; onDestroyView<\/p>\n<p>OF\u8ddfNF\u76f8\u90bb\uff1aonCreateView &gt; onViewCreated &gt; onActivityCreated &gt; onStart &gt; onResume<\/p>\n<p>OF\u5939\u5728PF\u548cNF\u4e2d\u95f4\uff1a\u4e0d\u8c03\u7528\u4efb\u4f55\u751f\u547d\u5468\u671f\u65b9\u6cd5<\/p>\n<p>\u5982\u679c\u91cd\u5199\u4e86FragmentPagerAdapter\u7684DestroyItem\u65b9\u6cd5\uff0c\u5e76\u4e14\u76f8\u5173\u7684Fragment\u5df2\u7ecf\u52a0\u8f7d\u8fc7\uff1a<br \/>\n\u76f8\u4e92\u5207\u6362\u65f6\u53ea\u4f1a\u8c03\u7528setUserVisibleHint<\/p>\n<p>Fragment\u8fdb\u5165\u4e86\u8fd0\u884c\u72b6\u6001\uff1a<br \/>\nFragment\u5728\u8fdb\u5165\u8fd0\u884c\u72b6\u6001\u65f6\uff0c\u4ee5\u4e0b\u56db\u4e2a\u751f\u547d\u5468\u671f\u4f1a\u968f\u5b83\u6240\u5c5e\u7684Activity\u4e00\u8d77\u88ab\u8c03\u7528\uff1a<\/p>\n<p>onPause() &#8212;&gt; onStop() &#8212;&gt; onStart() &#8212;&gt; onResume()<\/p>\n<p>\u5173\u4e8eFragment\u7684onActivityResult\u65b9\u6cd5\uff1a<br \/>\n\u4f7f\u7528Fragment\u7684startActivity\u65b9\u6cd5\u65f6\uff0cFragmentActivity\u7684onActivityResult\u65b9\u6cd5\u4f1a\u56de\u8c03\u76f8\u5e94\u7684Fragment\u7684onActivityResult\u65b9\u6cd5\uff0c\u6240\u4ee5\u5728\u91cd\u5199FragmentActivity\u7684onActivityResult\u65b9\u6cd5\u65f6\uff0c\u6ce8\u610f\u8c03\u7528super.onActivityResult\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u7531\u4e8eFragment\u7684\u751f\u547d\u5468\u671f\u4e0eActivity\u7684\u751f\u547d\u5468\u671f\u6709\u7740\u7275\u626f\uff0c\u6240\u4ee5\u628a\u4e24\u8005 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-42328","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=42328"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42328\/revisions"}],"predecessor-version":[{"id":42329,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42328\/revisions\/42329"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=42328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=42328"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=42328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}