{"id":902,"date":"2011-09-15T12:18:16","date_gmt":"2011-09-15T04:18:16","guid":{"rendered":"http:\/\/leybreeze.com\/blog\/?p=902"},"modified":"2011-11-04T10:16:08","modified_gmt":"2011-11-04T02:16:08","slug":"%e7%89%87%e6%ae%b5%ef%bc%88fragment%ef%bc%89","status":"publish","type":"post","link":"http:\/\/leybreeze.com\/blog\/?p=902","title":{"rendered":"\u7247\u6bb5\uff08Fragment\uff09"},"content":{"rendered":"<p>\u4e00\u4e2a\u7247\u6bb5\u4ee3\u8868\u4e86\u4e00\u4e2aActivity\u7684\u4e00\u79cd\u884c\u4e3a\u6216\u662f\u5176\u7528\u6237\u754c\u9762\u7684\u4e00\u4e2a\u533a\u57df\u3002\u53ef\u4ee5\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6d3b\u52a8\u4e2d\u7ec4\u5408\u591a\u4e2a\u5206\u7247\u6765\u7ec4\u5efa\u4e00\u4e2a\u591a\u9762\u677fUI\uff0c\u5e76\u5728\u4e0d\u540c\u7684\u6d3b\u52a8\u4e2d\u591a\u6b21\u5229\u7528\u540c\u4e00\u4e2a\u5206\u7247\u3002\u53ef\u4ee5\u628a\u7247\u6bb5\u7406\u89e3\u4e3a\u4e00\u4e2a\u6d3b\u52a8\u7684\u4e00\u4e2a\u6a21\u5757\u5316\u90e8\u5206\uff0c\u6709\u5176\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f\uff0c\u63a5\u6536\u5176\u81ea\u5df1\u7684\u8f93\u5165\u4e8b\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u6d3b\u52a8\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u6216\u79fb\u9664\u4e00\u4e2a\u7247\u6bb5\u3002\uff08\u8bd1\u6ce8\uff1a\u7f51\u7edc\u4e0a\u4e5f\u6709\u201c\u788e\u7247\u201d\u7684\u8bd1\u6cd5\uff0c\u4e0d\u8fc7\u76ee\u524d\u8fd8\u5e76\u6ca1\u6709\u7edf\u4e00\u7684\u540d\u79f0\u3002\u5728\u8fd9\u91cc\u59d1\u4e14\u79f0\u4e3a\u201c\u7247\u6bb5\u201d\uff0c\u4e2a\u4eba\u611f\u89c9\u6bd4\u201c\u788e\u7247\u201d\u542c\u8d77\u6765\u8212\u670d\u4e00\u4e9b :) \u00a0\uff09<\/p>\n<p>\u4e00\u4e2a\u7247\u6bb5\u5fc5\u987b\u88ab\u5d4c\u5728\u4e00\u4e2a\u6d3b\u52a8\u4e2d\uff0c\u5b83\u7684\u751f\u547d\u5468\u671f\u4e0e\u8be5\u6d3b\u52a8\u7684\u6709\u7740\u7d27\u5bc6\u8054\u7cfb\u3002\u4f8b\u5982\uff0c\u5f53\u6d3b\u52a8\u88ab\u6682\u505c\uff08pause\uff09\uff0c\u8be5\u6d3b\u52a8\u4e2d\u6240\u6709\u7684\u7247\u6bb5\u4e5f\u4f1a\u6682\u505c\uff0c\u5f53\u6d3b\u52a8\u88ab\u9500\u6bc1\uff08destroy\uff09\uff0c\u5176\u4e2d\u6240\u6709\u7684\u7247\u6bb5\u4e5f\u4f1a\u88ab\u9500\u6bc1\u3002\u4e0d\u8fc7\uff0c\u5f53\u6d3b\u52a8\u5728\u8fd0\u884c\u65f6\uff08\u5904\u4e8eresumed\u751f\u547d\u5468\u671f\u72b6\u6001\uff09\uff0c\u53ef\u4ee5\u5355\u72ec\u6539\u53d8\u6bcf\u4e00\u4e2a\u7247\u6bb5\uff0c\u5982\u6dfb\u52a0\u6216\u662f\u5220\u9664\u5b83\u4eec\u3002\u5f53\u8fdb\u884c\u8fd9\u6837\u7684\u7247\u6bb5\u5904\u7406\u65f6\uff0c\u8fd8\u80fd\u5c06\u7247\u6bb5\u52a0\u5165\u4e00\u4e2a\u7531\u8be5\u6d3b\u52a8\u7ba1\u7406\u7684\u8fd4\u56de\u6808\u2014\u2014\u6bcf\u4e2a\u6d3b\u52a8\u4e2d\u7684\u8fd4\u56de\u6808\u6761\u76ee\u662f\u4e00\u6bb5\u53d1\u751f\u8fc7\u7684\u7247\u6bb5\u5904\u7406\u7684\u8bb0\u5f55\u3002\u8fd4\u56de\u6808\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u6309\u4e0bBACK\u952e\u64a4\u9500\u4e00\u4e2a\u7247\u6bb5\u4e8b\u52a1\uff08\u53cd\u5411\u5bfc\u822a\uff09\u3002<\/p>\n<p>\u5728\u6dfb\u52a0\u4e00\u4e2a\u7247\u6bb5\u4f5c\u4e3a\u6d3b\u52a8\u5e03\u5c40\u7684\u4e00\u90e8\u5206\u65f6\uff0c\u5b83\u5c06\u5b58\u5728\u4e8e\u6d3b\u52a8\u7684\u89c6\u56fe\u5c42\u7ea7\u7684ViewGroup\u4e2d\uff0c\u5e76\u5b9a\u4e49\u5176\u81ea\u6709\u7684\u89c6\u56fe\u5e03\u5c40\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u6d3b\u52a8\u5e03\u5c40\u6587\u4ef6\u4e2d\u4ee5<span style=\"color: #008000;\">&lt;fragment&gt;<\/span>\u5143\u7d20\u58f0\u660e\u7247\u6bb5\uff0c\u6216\u662f\u5728\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u6dfb\u52a0\u81f3\u5df2\u6709\u7684ViewGroup\uff09\u6765\u5c06\u4e00\u4e2a\u7247\u6bb5\u63d2\u5165\u6d3b\u52a8\u7684\u5e03\u5c40\u4e4b\u4e2d\u3002\u4e0d\u8fc7\uff0c\u7247\u6bb5\u5e76\u4e0d\u4e00\u5b9a\u8981\u662f\u6d3b\u52a8\u5e03\u5c40\u7684\u4e00\u90e8\u5206\uff1b\u8fd8\u53ef\u4ee5\u5c06\u7247\u6bb5\u4f5c\u4e3a\u4e00\u4e2a\u6d3b\u52a8\u7684\u4e0d\u53ef\u89c1\u90e8\u5206\u4f7f\u7528\u3002<\/p>\n<p>\u672c\u6587\u6863\u63cf\u8ff0\u4e86\u5982\u4f55\u4f7f\u7528\u7247\u6bb5\u6765\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec\u7247\u6bb5\u5982\u4f55\u5728\u88ab\u52a0\u5165\u6d3b\u52a8\u7684\u8fd4\u56de\u6808\u65f6\u7ef4\u62a4\u5176\u72b6\u6001\uff0c\u4e0e\u6d3b\u52a8\u53ca\u6d3b\u52a8\u5185\u7684\u5176\u4ed6\u7247\u6bb5\u5171\u4eab\u4e8b\u4ef6\uff0c\u4ee5\u53ca\u548c\u6d3b\u52a8\u7684\u52a8\u4f5c\u6761\u76f8\u7ed3\u5408\u7b49\u3002<\/p>\n<h3><strong>\u8bbe\u8ba1\u7406\u5ff5<\/strong><\/h3>\n<p>Android\u5728Android 3.0\uff08API\u7ea7\u522b\u201cHoneycomb\u201d\u8702\u5de2\uff09\u4e2d\u5f15\u5165\u4e86\u7247\u6bb5\uff0c\u7528\u4ee5\u5728\u5e73\u677f\u7b49\u5927\u5c4f\u5e55\u4e0a\u652f\u6301\u66f4\u4e3a\u52a8\u6001\u800c\u7075\u6d3b\u7684UI\u8bbe\u8ba1\u3002\u7531\u4e8e\u5e73\u677f\u7b49\u5c4f\u5e55\u6bd4\u624b\u673a\u7684\u8981\u5927\u8bb8\u591a\uff0c\u56e0\u6b64\u6709\u66f4\u591a\u7684\u7a7a\u95f4\u6765\u4ea4\u4e92\u7ec4\u5408UI\u7ec4\u4ef6\u3002\u7247\u6bb5\u53ef\u4ee5\u5728\u4e0d\u5fc5\u8003\u8651\u89c6\u56fe\u5c42\u7ea7\u7684\u590d\u6742\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\u5b9e\u73b0\u8fd9\u79cd\u8bbe\u8ba1\u3002\u901a\u8fc7\u628a\u6d3b\u52a8\u7684\u5e03\u5c40\u5206\u6210\u4e00\u4e2a\u4e2a\u7247\u6bb5\uff0c\u5c31\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6539\u53d8\u6d3b\u52a8\u7684\u5916\u89c2\u5e76\u5728\u8be5\u6d3b\u52a8\u6240\u7ba1\u7406\u7684\u8fd4\u56de\u6808\u4e2d\u4fdd\u5b58\u8fd9\u4e9b\u53d8\u5316\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4e00\u4e2a\u65b0\u95fb\u7a0b\u5e8f\u53ef\u4ee5\u5728\u5de6\u4fa7\u7528\u4e00\u4e2a\u7247\u6bb5\u6765\u5c55\u793a\u6761\u76ee\u5217\u8868\uff0c\u5728\u53e6\u4e00\u8fb9\u7684\u7247\u6bb5\u4e2d\u663e\u793a\u4e00\u6761\u6761\u76ee\u2014\u2014\u4e24\u4e2a\u7247\u6bb5\u540c\u65f6\u663e\u793a\u5728\u4e00\u4e2a\u6d3b\u52a8\u4e2d\u7684\u4e24\u8fb9\uff0c\u4e14\u90fd\u6709\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f\u56de\u9988\u65b9\u6cd5\u5e76\u5904\u7406\u5176\u81ea\u6709\u7684\u7528\u6237\u8f93\u5165\u4e8b\u4ef6\u3002\u56e0\u6b64\uff0c\u76f8\u6bd4\u4e00\u4e2a\u6d3b\u52a8\u9009\u62e9\u6761\u76ee\u53e6\u4e00\u4e2a\u6d3b\u52a8\u9605\u8bfb\u6761\u76ee\uff0c\u73b0\u5728\u7528\u6237\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u6d3b\u52a8\u4e2d\u9009\u62e9\u6761\u76ee\u5e76\u9605\u8bfb\uff0c\u5c31\u50cf\u56fe1\u6240\u793a\u7684\u90a3\u6837\u3002<\/p>\n<figure id=\"attachment_914\" aria-describedby=\"caption-attachment-914\" style=\"width: 555px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/09\/fragments.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-914\" title=\"fragments\" src=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/09\/fragments.png\" alt=\"\" width=\"555\" height=\"174\" srcset=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/09\/fragments.png 555w, http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/09\/fragments-300x94.png 300w\" sizes=\"(max-width: 555px) 85vw, 555px\" \/><\/a><figcaption id=\"caption-attachment-914\" class=\"wp-caption-text\">\u56fe1 \u4e00\u4e2a\u6f14\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u7247\u6bb5\u5c06\u4e24\u4e2a\u72ec\u7acb\u7684UI\u6a21\u5757\u5e76\u5165\u4e00\u4e2a\u6d3b\u52a8\u7684\u8303\u4f8b<\/figcaption><\/figure>\n<p>\u4e00\u4e2a\u7247\u6bb5\u5e94\u8be5\u662f\u7a0b\u5e8f\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\u5316\u7684\u53ef\u91cd\u7528\u7684\u7ec4\u4ef6\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u56e0\u4e3a\u7247\u6bb5\u5b9a\u4e49\u4e86\u5176\u81ea\u6709\u7684\u5e03\u5c40\u4ee5\u53ca\u4f7f\u7528\u4e86\u81ea\u6709\u751f\u547d\u5468\u671f\u56de\u9988\u65b9\u6cd5\u7684\u81ea\u6709\u884c\u4e3a\uff0c\u6240\u4ee5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u7247\u6bb5\u7528\u5728\u591a\u4e2a\u6d3b\u52a8\u4e4b\u4e2d\u3002\u8fd9\u662f\u5f88\u91cd\u8981\u7684\u4e00\u70b9\uff0c\u5b83\u4f7f\u5f97\u80fd\u591f\u5728\u4e0d\u540c\u5c4f\u5e55\u5c3a\u5bf8\u4e0a\u63d0\u4f9b\u4e0d\u540c\u7684\u7528\u6237\u4f53\u9a8c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u53ea\u5728\u5c4f\u5e55\u5c3a\u5bf8\u8db3\u591f\u5927\u65f6\u624d\u5728\u4e00\u4e2a\u6d3b\u52a8\u4e2d\u5305\u542b\u591a\u4e2a\u7247\u6bb5\uff0c\u5426\u5219\uff0c\u5c31\u5c06\u4e0d\u540c\u7247\u6bb5\u5206\u5728\u4e0d\u540c\u7684\u6d3b\u52a8\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u4f8b\u5982\u2014\u2014\u8fd8\u662f\u90a3\u4e2a\u65b0\u95fb\u7a0b\u5e8f\u7684\u4f8b\u5b50\u2014\u2014\u7a0b\u5e8f\u53ef\u4ee5\u5728\u8fd0\u884c\u4e8e\u4e00\u4e2a\u8d85\u5927\u5c4f\u5e55\u8bbe\u5907\uff08extra large screen\uff0c\u6bd4\u5982\u5e73\u677f\u7535\u8111\uff09\u65f6\u5c06\u4e24\u4e2a\u7247\u6bb5\u5d4c\u5165\u5728\u4e00\u4e2a<em>Activity A<\/em>\u4e2d\u3002\u4e0d\u8fc7\uff0c\u5728\u666e\u901a\u5c3a\u5bf8\u5c4f\u5e55\u7684\u8bbe\u5907\uff08\u4f8b\u5982\uff0c\u624b\u673a\uff09\u4e0a\uff0c\u5c31\u6ca1\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u653e\u4e0b\u4e24\u4e2a\u7247\u6bb5\uff0c\u6240\u4ee5<em>Activity A<\/em>\u4ec5\u5305\u542b\u4e86\u6761\u76ee\u5217\u8868\u529f\u80fd\u7684\u7247\u6bb5\uff0c\u800c\u5f53\u7528\u6237\u9009\u62e9\u4e86\u4e00\u4e2a\u6761\u76ee\u65f6\uff0c\u5b83\u5c06\u542f\u52a8\u5305\u542b\u4e86\u9605\u8bfb\u6761\u76ee\u7684\u7247\u6bb5\u7684<em>Activity B<\/em>\u3002\u56e0\u6b64\uff0c\u5982\u56fe1\u6240\u793a\u7a0b\u5e8f\u5c06\u540c\u65f6\u652f\u6301\u4e24\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u3002<\/p>\n<h3><strong>\u521b\u5efa\u4e00\u4e2a\u7247\u6bb5<\/strong><\/h3>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u7247\u6bb5\uff0c\u5fc5\u987b\u521b\u5efa\u4e00\u4e2aFragment\uff08\u6216\u5b83\u7684\u4e00\u4e2a\u5df2\u6709\u7684\u5b50\u7c7b\uff09\u7684\u5b50\u7c7b\u3002Fragment\u7c7b\u7684\u4ee3\u7801\u770b\u8d77\u6765\u548c\u4e00\u4e2aActivity\u5f88\u76f8\u4f3c\u3002\u5b83\u5305\u542b\u4e86\u548c\u4e00\u4e2a\u6d3b\u52a8\u7c7b\u4f3c\u7684\u56de\u9988\u65b9\u6cd5\uff0c\u4f8b\u5982onCreate()\u3001onStart()\u3001onPause()\u548conStop()\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5982\u679c\u8981\u628a\u4e00\u4e2a\u5df2\u6709\u7684Android\u7a0b\u5e8f\u6539\u4e3a\u901a\u8fc7\u7247\u6bb5\u6765\u5b9e\u73b0\uff0c\u53ea\u9700\u7b80\u5355\u5730\u628a\u4ee3\u7801\u4e2d\u7684\u6d3b\u52a8\u7684\u56de\u9988\u65b9\u6cd5\u6539\u4e3a\u76f8\u5e94\u7684\u7247\u6bb5\u7684\u56de\u9988\u65b9\u6cd5\u3002<\/p>\n<p>\u901a\u5e38\uff0c\u81f3\u5c11\u9700\u8981\u5b9e\u73b0\u4ee5\u4e0b\u7684\u751f\u547d\u5468\u671f\u65b9\u6cd5\uff1a<\/p>\n<p>onCreate()<\/p>\n<p style=\"padding-left: 30px;\">\u7cfb\u7edf\u5728\u521b\u5efa\u7247\u6bb5\u65f6\u5c06\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002\u5728\u5176\u5b9e\u73b0\u4e2d\uff0c\u5e94\u5f53\u521d\u59cb\u5316\u90a3\u4e9b\u5e0c\u671b\u5728\u8be5\u7247\u6bb5\u6682\u505c\u6216\u505c\u6b62\u65f6\u88ab\u4fdd\u7559\u7684\u5fc5\u8981\u7ec4\u4ef6\u4ee5\u4f9b\u4e4b\u540e\u7ee7\u7eed\u4f7f\u7528\u3002<\/p>\n<p>onCreateView()<\/p>\n<p style=\"padding-left: 30px;\">\u7cfb\u7edf\u5728\u7247\u6bb5\u7b2c\u4e00\u6b21\u7ed8\u5236\u5176\u7528\u6237\u754c\u9762\u65f6\u5c06\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002\u8981\u7ed8\u5236\u7247\u6bb5\u7684UI\uff0c\u5c31\u5fc5\u987b\u4ece\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u7247\u6bb5\u5e03\u5c40\u7684\u6839View\u3002\u5982\u679c\u7247\u6bb5\u4e0d\u63d0\u4f9bUI\uff0c\u53ef\u4ee5\u53ea\u8fd4\u56de\u4e00\u4e2anull\u3002<\/p>\n<p>onPause()<\/p>\n<p style=\"padding-left: 30px;\">\u7cfb\u7edf\u5c06\u8c03\u7528\u8be5\u65b9\u6cd5\u4f5c\u4e3a\u7528\u6237\u5c06\u8981\u79bb\u5f00\u8be5\u7247\u6bb5\u7684\u7b2c\u4e00\u4e2a\u6807\u5fd7\uff08\u5c3d\u7ba1\u8fd9\u4e0d\u610f\u5473\u7740\u7247\u6bb5\u4e00\u5b9a\u5c31\u4f1a\u88ab\u9500\u6bc1\uff09\u3002\u901a\u5e38\u5e94\u5f53\u5728\u8fd9\u91cc\u4fdd\u5b58\u5f53\u524d\u7528\u6237\u8fdb\u884c\u7684\u64cd\u4f5c\uff08\u56e0\u4e3a\u7528\u6237\u4e4b\u540e\u6216\u8bb8\u4e0d\u4f1a\u8fd4\u56de\u8be5\u7247\u6bb5\u4e86\uff09\u3002<\/p>\n<p>\u5927\u90e8\u5206\u7684\u7a0b\u5e8f\u5e94\u5f53\u4e3a\u6bcf\u4e00\u4e2a\u7247\u6bb5\u81f3\u5c11\u5b9e\u73b0\u4ee5\u4e0a\u4e09\u4e2a\u65b9\u6cd5\uff0c\u4e0d\u8fc7\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u56de\u9988\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u5904\u7406\u7247\u6bb5\u751f\u547d\u5468\u671f\u7684\u4e0d\u540c\u9636\u6bb5\u3002\u6240\u6709\u7684\u751f\u547d\u5468\u671f\u56de\u9988\u65b9\u6cd5\u4f1a\u5728\u4e4b\u540e\u7684\u201c\u5904\u7406\u7247\u6bb5\u751f\u547d\u5468\u671f\u201d\u4e00\u8282\u518d\u8ba8\u8bba\u3002<\/p>\n<figure id=\"attachment_922\" aria-describedby=\"caption-attachment-922\" style=\"width: 329px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/09\/fragment_lifecycle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-922\" title=\"fragment_lifecycle\" src=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/09\/fragment_lifecycle.png\" alt=\"\" width=\"329\" height=\"792\" srcset=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/09\/fragment_lifecycle.png 329w, http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/09\/fragment_lifecycle-124x300.png 124w\" sizes=\"(max-width: 329px) 85vw, 329px\" \/><\/a><figcaption id=\"caption-attachment-922\" class=\"wp-caption-text\">\u56fe2. \u4e00\u4e2a\u7247\u6bb5\u7684\u751f\u547d\u5468\u671f\uff08\u5f53\u5176\u6240\u5c5e\u7684\u6d3b\u52a8\u5904\u4e8e\u8fd0\u884c\u72b6\u6001\u65f6\uff09<\/figcaption><\/figure>\n<p>\u9664\u4e86Fragment\u57fa\u7c7b\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u53ef\u4f9b\u7ee7\u627f\u7684\u5b50\u7c7b\uff1a<\/p>\n<p>DialogFragment<\/p>\n<p style=\"padding-left: 30px;\">\u663e\u793a\u4e00\u4e2a\u6d6e\u52a8\u5bf9\u8bdd\u6846\u3002\u53ef\u4ee5\u7528\u8be5\u7c7b\u6765\u521b\u5efa\u5bf9\u8bdd\u6846\u800c\u4e0d\u662f\u7528Acitivity\u7c7b\u4e2d\u7684\u5bf9\u8bdd\u6846\u8f85\u52a9\u65b9\u6cd5\u6765\u521b\u5efa\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u4e00\u4e2a\u7247\u6bb5\u5bf9\u8bdd\u6846\u52a0\u5165\u7531\u6d3b\u52a8\u6240\u7ba1\u7406\u7684\u7247\u6bb5\u7684\u8fd4\u56de\u6808\uff0c\u4ee4\u7528\u6237\u53ef\u4ee5\u8fd4\u56de\u5230\u4e00\u4e2a\u5df2\u88ab\u820d\u5f03\u7684\u7247\u6bb5\u3002<\/p>\n<p>ListFragment<\/p>\n<p style=\"padding-left: 30px;\">\u663e\u793a\u4e00\u4e2a\u7531\u67d0\u4e00\u9002\u914d\u5668\uff08\u4f8b\u5982SimpleCursorAdapter\uff09\u7ba1\u7406\u7684\u9879\u76ee\u5217\u8868\uff0c\u7c7b\u4f3c\u4e8eListActivity\u3002\u5b83\u63d0\u4f9b\u4e86\u597d\u51e0\u79cd\u7ba1\u7406\u5217\u8868\u89c6\u56fe\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982onListItemClick()\u56de\u9988\u65b9\u6cd5\u4ee5\u5904\u7406\u70b9\u51fb\u4e8b\u4ef6\u3002<\/p>\n<p>PreferenceFragment\uff08\u504f\u597d\u8bbe\u7f6e\u7247\u6bb5\uff09<\/p>\n<p style=\"padding-left: 30px;\">\u4ee5\u5217\u8868\u65b9\u5f0f\u663e\u793a\u4e00\u4e2aPreference\u5bf9\u8c61\u7684\u5c42\u7ea7\uff0c\u7c7b\u4f3c\u4e8ePreferenceActivity\u3002\u5f53\u4e3a\u7a0b\u5e8f\u521b\u5efa\u201c\u8bbe\u7f6e\u201d\u6d3b\u52a8\u65f6\u8fd9\u662f\u5f88\u6709\u7528\u7684\u3002<\/p>\n<h3><strong>\u6dfb\u52a0\u7528\u6237\u754c\u9762<\/strong><\/h3>\n<p>\u4e00\u4e2a\u7247\u6bb5\u901a\u5e38\u88ab\u7528\u4e8e\u4e00\u4e2a\u6d3b\u52a8\u7684\u7528\u6237\u754c\u9762\uff0c\u5176\u81ea\u6709\u7684\u5e03\u5c40\u5c06\u6210\u4e3a\u8be5\u6d3b\u52a8\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<p>\u8981\u4e3a\u4e00\u4e2a\u7247\u6bb5\u63d0\u4f9b\u5e03\u5c40\uff0c\u5c31\u5fc5\u987b\u5b9e\u73b0onCreateView()\u56de\u9988\u65b9\u6cd5\uff0cAndroid\u7cfb\u7edf\u5c06\u5728\u8be5\u7247\u6bb5\u7ed8\u5236\u5176\u5e03\u5c40\u65f6\u8c03\u7528\u5b83\u3002\u8be5\u65b9\u6cd5\u7684\u5b9e\u73b0\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u7247\u6bb5\u5e03\u5c40\u7684\u6839View\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u5982\u679c\u7247\u6bb5\u662f\u4e00\u4e2aListFragment\u7684\u5b50\u7247\u6bb5\uff0c\u90a3\u4e48\u9ed8\u8ba4\u7684\u5b9e\u73b0\u5c06\u4eceonCreateView()\u8fd4\u56de\u4e00\u4e2aListView\uff0c\u56e0\u6b64\u4e0d\u5fc5\u53e6\u5916\u53bb\u5b9e\u73b0\u8be5\u65b9\u6cd5\u3002<\/p>\n<p>\u8981\u4eceonCreateView()\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c40\uff0c\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5b9a\u4e49\u4e8eXML\u4e2d\u7684\u5e03\u5c40\u8d44\u6e90\u4e2d\u751f\u6210\u5b83\u3002\u4e3a\u6b64\uff0conCreateView()\u63d0\u4f9b\u4e86\u4e00\u4e2aLayoutInflater\u5bf9\u8c61\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2aFragment\u7684\u5b50\u7c7b\u4ece<span style=\"color: #008000;\">example_fragment.xml<\/span>\u6587\u4ef6\u4e2d\u8bfb\u53d6\u4e86\u4e00\u4e2a\u5e03\u5c40\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">public<\/span> <span style=\"color: #000080;\">static class<\/span> <span style=\"color: #800080;\">ExampleFragment<\/span> <span style=\"color: #000080;\">extends<\/span> <span style=\"color: #800080;\">Fragment<\/span> {<\/span>\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public<\/span> <span style=\"color: #800080;\">View<\/span> onCreateView(<span style=\"color: #800080;\">LayoutInflater<\/span> inflater, <span style=\"color: #800080;\">ViewGroup<\/span> container,<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Bundle<\/span> savedInstanceState) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ Inflate the layout for this fragment<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 return<\/span> inflater.inflate(R.layout.example_fragment, container, <span style=\"color: #000080;\">false<\/span>);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u88ab\u4f20\u9012\u7ed9onCreateView()\u7684<span style=\"color: #008000;\">container<\/span>\u53c2\u6570\u662f\u4e0a\u4e00\u7ea7\u7684ViewGroup\uff08\u6765\u81ea\u4e8e\u8be5\u6d3b\u52a8\u7684\u5e03\u5c40\uff09\uff0c\u7247\u6bb5\u7684\u5e03\u5c40\u5c06\u88ab\u63d2\u5165\u5176\u4e2d\u3002<span style=\"color: #008000;\">savedInstanceState<\/span>\u53c2\u6570\u662f\u5728\u8fd4\u56de\u7247\u6bb5\u65f6\u63d0\u4f9b\u4e4b\u524d\u7247\u6bb5\u5b9e\u4f8b\u6570\u636e\u7684\u4e00\u4e2aBundle\uff08\u5c06\u5728\u201c\u5904\u7406\u7247\u6bb5\u751f\u547d\u5468\u671f\u201d\u4e00\u8282\u4e2d\u66f4\u6df1\u5165\u5730\u8ba8\u8bba\u72b6\u6001\u6062\u590d\uff09\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u521b\u5efa\u4e00\u4e2a\u5e03\u5c40<\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u5728\u4e0a\u9762\u7684\u8303\u4f8b\u4e2d\uff0c<span style=\"color: #008000;\">R.layout.example_fragment<\/span>\u662f\u6307\u5411\u4fdd\u5b58\u4e8e\u7a0b\u5e8f\u8d44\u6e90\u4e2d\u7684\u4e00\u4e2a\u540d\u4e3a<span style=\"color: #008000;\">example_fragment.xml<\/span>\u5e03\u5c40\u8d44\u6e90\u7684\u5f15\u7528\u3002\u5173\u4e8e\u5982\u4f55\u5728XML\u4e2d\u521b\u5efa\u4e00\u4e2a\u5e03\u5c40\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1\u201c<a title=\"\u7528\u6237\u754c\u9762\" href=\"http:\/\/leybreeze.com\/blog\/?p=117\">\u7528\u6237\u754c\u9762<\/a>\u201d\u6587\u6863\u3002<\/p>\n<p>inflate()\u65b9\u6cd5\u9700\u8981\u4e09\u4e2a\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>\u5e0c\u671b\u751f\u6210\u7684\u5e03\u5c40\u7684\u8d44\u6e90ID<\/li>\n<li>\u8981\u751f\u6210\u7684\u5e03\u5c40\u7684\u7236\u7ea7ViewGroup\u3002\u4e3a\u4e86\u4f7f\u7cfb\u7edf\u5c06\u5e03\u5c40\u53c2\u6570\u4f20\u9012\u7ed9\u751f\u6210\u7684\u5e03\u5c40\u7684\u6839\u89c6\u56fe\uff0c\u5c31\u9700\u8981\u4f20\u9012\u8be5<span style=\"color: #008000;\">container<\/span>\u3002\u8fd9\u5c06\u7531\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u7236\u89c6\u56fe\u6765\u51b3\u5b9a\u3002<\/li>\n<li>\u4e00\u4e2a\u8868\u660e\u4e86\u751f\u6210\u7684\u5e03\u5c40\u5728\u751f\u6210\u8fc7\u7a0b\u4e2d\u662f\u5426\u5e94\u8be5\u548cViewGroup\uff08\u5373\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff09\u76f8\u5173\u8054\u7684\u5e03\u5c14\u503c\u3002\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\uff0c\u56e0\u4e3a\u7cfb\u7edf\u5df2\u7ecf\u5c06\u751f\u6210\u7684\u5e03\u5c40\u63d2\u5165\u5230\u4e86<span style=\"color: #008000;\">container<\/span>\u5185\u6240\u4ee5\u503c\u4e3afalse\u2014\u2014\u5982\u679c\u662ftrue\u5219\u4f1a\u5728\u6700\u7ec8\u7684\u5e03\u5c40\u4e2d\u4ea7\u751f\u4e00\u4e2a\u5197\u4f59\u7684\u89c6\u56fe\u7ec4\u3002\uff09<\/li>\n<\/ul>\n<p>\u73b0\u5728\u5df2\u7ecf\u4e86\u89e3\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u63d0\u4f9b\u4e86\u5e03\u5c40\u7684\u7247\u6bb5\u3002\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u5c06\u8be5\u7247\u6bb5\u6dfb\u52a0\u5230\u6d3b\u52a8\u4e2d\u53bb\u3002<\/p>\n<h3><strong>\u5411\u6d3b\u52a8\u6dfb\u52a0\u4e00\u4e2a\u7247\u6bb5<\/strong><\/h3>\n<p>\u901a\u5e38\uff0c\u7247\u6bb5\u4f5c\u4e3a\u5bbf\u4e3b\u6d3b\u52a8\u7684UI\u7684\u4e00\u90e8\u5206\u5d4c\u4e8e\u8be5\u6d3b\u52a8\u6574\u4f53\u89c6\u56fe\u5c42\u7ea7\u4e4b\u4e2d\u3002\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5c06\u4e00\u4e2a\u7247\u6bb5\u6dfb\u52a0\u5230\u6d3b\u52a8\u7684\u5e03\u5c40\uff1a<\/p>\n<ul>\n<li><strong>\u5728\u6d3b\u52a8\u7684\u5e03\u5c40\u6587\u4ef6\u91cc\u58f0\u660e\u8be5\u7247\u6bb5\u3002<\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px;\">\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u7247\u6bb5\u662f\u4e00\u4e2a\u89c6\u56fe\uff0c\u53ef\u4ee5\u4e3a\u5176\u6307\u5b9a\u5e03\u5c40\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u542b\u6709\u4e24\u4e2a\u7247\u6bb5\u7684\u6d3b\u52a8\u7684\u5e03\u5c40\u6587\u4ef6\uff1a<\/p>\n<pre><span style=\"color: #008000;\"><span style=\"color: #808000;\">&lt;?<\/span><span style=\"color: #000000;\">xml version<\/span><span style=\"color: #808000;\">=<\/span>\"1.0\" <span style=\"color: #000000;\">encoding<span style=\"color: #808000;\">=<\/span><\/span>\"utf-8\"<span style=\"color: #808000;\">?&gt;<\/span><\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #333399;\">&lt;LinearLayout<\/span> <span style=\"color: #800080;\">xmlns:android<\/span><span style=\"color: #808000;\">=<\/span>\"http:\/\/schemas.android.com\/apk\/res\/android\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 android:orientation<\/span><span style=\"color: #808000;\">=<\/span>\"horizontal\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 android:layout_width<\/span><span style=\"color: #808000;\">=<\/span>\"match_parent\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 android:layout_height<\/span><span style=\"color: #808000;\">=<\/span>\"match_parent\"<span style=\"color: #333399;\">&gt;<\/span><\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #333399;\">\u00a0 \u00a0 &lt;fragment<\/span> <span style=\"color: #800080;\">android:name<\/span><span style=\"color: #808000;\">=<\/span>\"com.example.news.ArticleListFragment\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:id<\/span><span style=\"color: #808000;\">=<\/span>\"@+id\/list\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_weight<\/span><span style=\"color: #808000;\">=<\/span>\"1\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_width<\/span><span style=\"color: #808000;\">=<\/span>\"0dp\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_height<\/span><span style=\"color: #808000;\">=<\/span>\"match_parent\" <span style=\"color: #333399;\">\/&gt;<\/span><\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #333399;\">\u00a0 \u00a0 &lt;fragment<\/span> <span style=\"color: #800080;\">android:name<\/span><span style=\"color: #808000;\">=<\/span>\"com.example.news.ArticleReaderFragment\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:id<\/span><span style=\"color: #808000;\">=<\/span>\"@+id\/viewer\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_weight<\/span><span style=\"color: #808000;\">=<\/span>\"2\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_width<\/span><span style=\"color: #808000;\">=<\/span>\"0dp\"<\/span>\r\n<span style=\"color: #008000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_height<\/span><span style=\"color: #808000;\">=<\/span>\"match_parent\" <span style=\"color: #333399;\">\/&gt;<\/span><\/span>\r\n<span style=\"color: #333399;\">&lt;\/LinearLayout&gt;<\/span><\/pre>\n<p style=\"padding-left: 30px;\"><span style=\"color: #008000;\">\u00a0&lt;fragment&gt;<\/span>\u4e2d\u7684<span style=\"color: #008000;\">android:name<\/span>\u5c5e\u6027\u6307\u5b9a\u4e86\u7528\u6765\u5b9e\u4f8b\u5316\u5e03\u5c40\u7684Fragment\u7c7b\u3002<\/p>\n<p style=\"padding-left: 30px;\">\u5f53\u7cfb\u7edf\u521b\u5efa\u8be5\u6d3b\u52a8\u5e03\u5c40\u65f6\uff0c\u5c06\u5b9e\u4f8b\u5316\u5728\u5e03\u5c40\u4e2d\u6307\u5b9a\u7684\u6bcf\u4e00\u4e2a\u7247\u6bb5\u5e76\u4e3a\u5176\u8c03\u7528onCreateView()\u65b9\u6cd5\u6765\u68c0\u7d22\u6bcf\u4e00\u4e2a\u7247\u6bb5\u7684\u5e03\u5c40\u3002\u7cfb\u7edf\u5c06\u5728<span style=\"color: #008000;\">&lt;fragment&gt;<\/span>\u5143\u7d20\u5904\u76f4\u63a5\u63d2\u5165\u7531\u8be5\u7247\u6bb5\u8fd4\u56de\u7684View\u3002<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u6bcf\u4e00\u4e2a\u7247\u6bb5\u9700\u8981\u5728\u7cfb\u7edf\u4e2d\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7528\u4ee5\u5728\u6d3b\u52a8\u91cd\u542f\u65f6\u8fd8\u539f\u8be5\u7247\u6bb5\uff08\u5e76\u4e14\u7528\u4e8e\u83b7\u53d6\u8be5\u7247\u6bb5\u4ee5\u6267\u884c\u5982\u79fb\u9664\u8be5\u7247\u6bb5\u4e4b\u7c7b\u7684\u64cd\u4f5c\uff09\u3002\u6709\u4e09\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e00\u4e2a\u7247\u6bb5\u7684ID\uff1a<\/p>\n<ul>\n<ul>\n<li>\u4e3a<span style=\"color: #008000;\">android:id<\/span>\u5c5e\u6027\u63d0\u4f9b\u552f\u4e00\u7684ID\u3002<\/li>\n<li>\u4e3a<span style=\"color: #008000;\">android:tag<\/span>\u5c5e\u6027\u63d0\u4f9b\u552f\u4e00\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u4ee5\u4e0a\u4e24\u79cd\u6807\u8bc6\uff0c\u7cfb\u7edf\u5c06\u4f7f\u7528\u5176\u5bb9\u5668\u89c6\u56fe\u7684ID\u3002<\/li>\n<\/ul>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong>\u6216\u8005\uff0c\u5728\u7a0b\u5e8f\u4e2d\u5c06\u7247\u6bb5\u6dfb\u52a0\u81f3\u5df2\u6709\u7684ViewGroup\u3002<\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px;\">\u5728\u6d3b\u52a8\u6b63\u5728\u8fd0\u884c\u4e2d\u7684\u4efb\u610f\u65f6\u523b\uff0c\u90fd\u53ef\u4ee5\u5c06\u7247\u6bb5\u6dfb\u52a0\u81f3\u6d3b\u52a8\u7684\u5e03\u5c40\u3002\u53ea\u9700\u8981\u6307\u5b9a\u8fd9\u4e00\u9700\u8981\u653e\u7f6e\u7247\u6bb5\u7684ViewGroup\u5373\u53ef\u3002<\/p>\n<p style=\"padding-left: 30px;\">\u8981\u5728\u6d3b\u52a8\u4e2d\u8fdb\u884c\u7247\u6bb5\u4e8b\u52a1\uff08\u4f8b\u5982\u6dfb\u52a0\u3001\u79fb\u9664\u6216\u66ff\u6362\u4e00\u4e2a\u7247\u6bb5\uff09\uff0c\u5fc5\u987b\u4f7f\u7528FragmentTransaction\u6240\u63d0\u4f9b\u7684API\u3002\u53ef\u4ee5\u50cf\u8fd9\u6837\u5728Activity\u4e2d\u83b7\u53d6\u4e00\u4e2aFragmentTransaction\u7684\u5b9e\u4f8b\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">FragmentManager<\/span> fragmentManager = <code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#getFragmentManager()\"><span style=\"color: #000000;\">getFragmentManager()<\/span><\/a><\/code><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">FragmentTransaction<\/span> fragmentTransaction = fragmentManager.<code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/FragmentManager.html#beginTransaction()\"><span style=\"color: #000000;\">beginTransaction()<\/span><\/a><\/code>;<\/span><\/pre>\n<p style=\"padding-left: 30px;\">\u4e4b\u540e\u53ef\u4ee5\u7528add()\u65b9\u6cd5\u6dfb\u52a0\u4e00\u4e2a\u7247\u6bb5\uff0c\u6307\u5b9a\u8981\u6dfb\u52a0\u7684\u7247\u6bb5\u4ee5\u53ca\u8981\u63d2\u5165\u8be5\u7247\u6bb5\u7684\u89c6\u56fe\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">ExampleFragment<\/span> fragment = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">ExampleFragment<\/span>();<\/span>\r\n<span style=\"color: #000000;\">fragmentTransaction.add(R.id.fragment_container, fragment);<\/span>\r\n<span style=\"color: #000000;\">fragmentTransaction.commit();<\/span><\/pre>\n<p style=\"padding-left: 30px;\">\u4f20\u9012\u7ed9add()\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8be5\u7247\u6bb5\u5e94\u5f53\u88ab\u653e\u7f6e\u7684ViewGroup\uff0c\u901a\u8fc7\u8d44\u6e90ID\u6765\u6307\u5b9a\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u662f\u8981\u6dfb\u52a0\u7684\u7247\u6bb5\u3002<\/p>\n<p style=\"padding-left: 30px;\">\u4e00\u65e6\u901a\u8fc7FragmentTransaction\u8fdb\u884c\u4e86\u53d8\u66f4\uff0c\u6bd4\u5982\u8c03\u7528commit()\u6765\u4f7f\u53d8\u66f4\u751f\u6548\u3002<\/p>\n<p><strong>\u6dfb\u52a0\u6ca1\u6709UI\u7684\u7247\u6bb5<\/strong><\/p>\n<p>\u4e0a\u9762\u7684\u8303\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u6dfb\u52a0\u63d0\u4f9b\u4e86UI\u7684\u7247\u6bb5\u81f3\u6d3b\u52a8\u4e2d\u3002\u4e0d\u8fc7\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4e00\u4e2a\u7247\u6bb5\u6765\u5728\u6d3b\u52a8\u4e2d\u6267\u884c\u540e\u53f0\u884c\u4e3a\u800c\u4e0d\u663e\u793a\u989d\u5916\u7684UI\u3002<\/p>\n<p>\u8981\u6dfb\u52a0\u6ca1\u6709UI\u7684\u7247\u6bb5\uff0c\u9700\u8981\u5728\u6d3b\u52a8\u4e2d\u901a\u8fc7\u4f7f\u7528add(Fragment, String)\u6765\u5b8c\u6210\uff08\u4e3a\u7247\u6bb5\u63d0\u4f9b\u4e00\u4e2a\u552f\u4e00\u7684&#8221;tag&#8221;\u5b57\u7b26\u4e32\u800c\u975e\u4e00\u4e2a\u89c6\u56feID\uff09\u3002\u8fd9\u6837\u5c31\u80fd\u6dfb\u52a0\u8be5\u7247\u6bb5\uff0c\u4f46\u662f\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u548c\u6d3b\u52a8\u5e03\u5c40\u4e2d\u7684\u67d0\u4e2a\u89c6\u56fe\u76f8\u5173\u8054\uff0c\u6240\u4ee5\u5c06\u4e0d\u4f1a\u6536\u5230onCreateView()\u7684\u8c03\u7528\u3002\u56e0\u6b64\u4e0d\u9700\u8981\u53bb\u5b9e\u73b0\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n<p>\u4e3a\u7247\u6bb5\u63d0\u4f9b\u4e00\u4e2a\u5b57\u7b26\u4e32\u6807\u7b7e\uff08tag\uff09\u5bf9\u4e8e\u65e0UI\u7247\u6bb5\u6765\u8bf4\u5e76\u4e0d\u662f\u5f88\u4e25\u683c\u7684\u8981\u6c42\u2014\u2014\u4e5f\u80fd\u4e3a\u5305\u542bUI\u7684\u7247\u6bb5\u63d0\u4f9b\u6807\u7b7e\u2014\u2014\u4f46\u5982\u679c\u7247\u6bb5\u4e0d\u542bUI\uff0c\u90a3\u4e48\u5b57\u7b26\u4e32\u6807\u7b7e\u5c31\u662f\u552f\u4e00\u80fd\u8bc6\u522b\u8be5\u7247\u6bb5\u7684\u65b9\u5f0f\u4e86\u3002\u5982\u679c\u5e0c\u671b\u4e4b\u540e\u80fd\u4ece\u6d3b\u52a8\u4e2d\u83b7\u53d6\u8be5\u7247\u6bb5\uff0c\u9700\u8981\u4f7f\u7528findFragmentByTag()\u3002<\/p>\n<p>\u5173\u4e8e\u5c06\u7247\u6bb5\u4f5c\u4e3a\u540e\u53f0\u5de5\u4f5c\u90e8\u4ef6\u800c\u4e0d\u542bUI\u7684\u6d3b\u52a8\u7684\u8303\u4f8b\uff0c\u8bf7\u53c2\u89c1<a title=\"FragmentRetainInstance.java\" href=\"http:\/\/developer.android.com\/resources\/samples\/ApiDemos\/src\/com\/example\/android\/apis\/app\/FragmentRetainInstance.html\" target=\"_blank\">FragmentRetainInstance.java<\/a>\u8303\u4f8b\u3002<\/p>\n<h3><strong>\u7ba1\u7406\u7247\u6bb5<\/strong><\/h3>\n<p>\u8981\u5728\u6d3b\u52a8\u4e2d\u7ba1\u7406\u7247\u6bb5\uff0c\u9700\u8981\u4f7f\u7528FragmentManager\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u6d3b\u52a8\u4e2d\u8c03\u7528getFragmentManager()\u6765\u83b7\u53d6\u5b83\u3002<\/p>\n<p>\u501f\u52a9FragmentManager\u53ef\u4ee5\u505a\u5230\u5305\u62ec\u4ee5\u4e0b\u8fd9\u4e9b\u4e8b\uff1a<\/p>\n<ul>\n<li>\u901a\u8fc7findFragmentById()\uff08\u9002\u7528\u4e8e\u5728\u6d3b\u52a8\u5e03\u5c40\u4e2d\u63d0\u4f9b\u4e86UI\u7684\u7247\u6bb5\uff09\u6216findFragmentByTag()\uff08\u5bf9\u4e8e\u63d0\u4f9b\u4e86\u6216\u6ca1\u6709\u63d0\u4f9bUI\u7684\u7247\u6bb5\u90fd\u9002\u7528\uff09\u6765\u83b7\u53d6\u5df2\u5b58\u5728\u4e8e\u6d3b\u52a8\u4e4b\u4e2d\u7684\u7247\u6bb5\u3002<\/li>\n<li>\u901a\u8fc7popBackStack()\u5c06\u7247\u6bb5\u4ece\u8fd4\u56de\u6808\u4e2d\u5f39\u51fa\uff08\u6a21\u62df\u4e86\u4e00\u6b21\u7528\u6237\u6309\u4e0bBACK\u952e\u7684\u6307\u4ee4\uff09\u3002<\/li>\n<li>\u901a\u8fc7addOnBackStackChangedListener()\u4e3a\u8fd4\u56de\u6808\u7684\u53d8\u5316\u6ce8\u518c\u4e00\u4e2a\u76d1\u542c\u5668\u3002<\/li>\n<\/ul>\n<p>\u5173\u4e8e\u8fd9\u4e9b\u65b9\u6cd5\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1FragmentManager\u7c7b\u7684\u6587\u6863\u3002<\/p>\n<p>\u6b63\u5982\u5728\u4e4b\u524d\u4e00\u8282\u4e2d\u6240\u63cf\u8ff0\u7684\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528FragmentManager\u6765\u6253\u5f00\u4e00\u4e2aFragmentTransaction\uff0c\u4ee5\u80fd\u591f\u6267\u884c\u4f8b\u5982\u6dfb\u52a0\u3001\u79fb\u9664\u7247\u6bb5\u7b49\u4e8b\u52a1\u3002<\/p>\n<h3><strong>\u6267\u884c\u7247\u6bb5\u4e8b\u52a1\uff08Fragment Transaction\uff09<\/strong><\/h3>\n<p>\u5728\u6d3b\u52a8\u4e2d\u4f7f\u7528\u7247\u6bb5\u7684\u4e00\u5927\u7279\u70b9\u662f\u53ef\u4ee5\u6839\u636e\u7528\u6237\u4ea4\u4e92\u5bf9\u8fd9\u4e9b\u7247\u6bb5\u8fdb\u884c\u6dfb\u52a0\u3001\u79fb\u9664\u3001\u66ff\u6362\u6216\u662f\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002\u5bf9\u6d3b\u52a8\u8fdb\u884c\u7684\u6bcf\u4e00\u7ec4\u6539\u53d8\u90fd\u88ab\u79f0\u4e3a\u662f\u4e00\u6b21\u4e8b\u52a1\uff08transaction\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7FragmentTransaction\u63d0\u4f9b\u7684API\u6267\u884c\u4e8b\u52a1\u3002\u8fd8\u53ef\u4ee5\u628a\u6bcf\u4e00\u4e2a\u4e8b\u52a1\u90fd\u4fdd\u5b58\u81f3\u6d3b\u52a8\u6240\u7ba1\u7406\u7684\u8fd4\u56de\u6808\uff0c\u4f7f\u5f97\u7528\u6237\u53ef\u4ee5\u64a4\u9500\u7247\u6bb5\u7684\u6539\u53d8\uff08\u548c\u8fd4\u56de\u4e0a\u4e00\u4e2a\u6d3b\u52a8\u7c7b\u4f3c\uff09\u3002<\/p>\n<p>\u53ef\u4ee5\u50cf\u8fd9\u6837\u4eceFragmentManager\u83b7\u53d6\u4e00\u4e2aFragmentTransaction\u7684\u5b9e\u4f8b\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">FragmentManager<\/span> fragmentManager = <code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#getFragmentManager()\"><span style=\"color: #000000;\">getFragmentManager()<\/span><\/a><\/code>;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">FragmentTransaction<\/span> fragmentTransaction = fragmentManager.<code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/FragmentManager.html#beginTransaction()\"><span style=\"color: #000000;\">beginTransaction()<\/span><\/a><\/code>;<\/span><\/pre>\n<p>\u6bcf\u4e00\u4e2a\u4e8b\u52a1\u90fd\u662f\u4e00\u7ec4\u80fd\u540c\u65f6\u6267\u884c\u7684\u53d8\u66f4\u3002\u53ef\u4ee5\u901a\u8fc7\u5bf9\u7ed9\u5b9a\u64cd\u4f5c\u4f7f\u7528\u5982add()\u3001remove()\u548creplace()\u7b49\u65b9\u6cd5\u5728\u8bbe\u7f6e\u6240\u6709\u5e0c\u671b\u6267\u884c\u7684\u53d8\u66f4\u3002\u4e4b\u540e\uff0c\u5fc5\u987b\u5bf9\u6d3b\u52a8\u8c03\u7528commit()\u6765\u5e94\u7528\u8be5\u4e8b\u52a1\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u5728\u8c03\u7528commit()\u4e4b\u524d\uff0c\u9700\u8981\u8c03\u7528addToBackStack()\uff0c\u4ee5\u5c06\u8be5\u4e8b\u52a1\u52a0\u5165\u7247\u6bb5\u4e8b\u52a1\u7684\u8fd4\u56de\u6808\u4e2d\u3002\u8be5\u8fd4\u56de\u6808\u7531\u6d3b\u52a8\u6240\u7ba1\u7406\uff0c\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u6309\u4e0b\u8fd4\u56de\u952e\u6765\u8fd4\u56de\u5230\u4e4b\u524d\u7684\u7247\u6bb5\u72b6\u6001\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4e0b\u9762\u5c55\u793a\u4e86\u5982\u4f55\u66ff\u6362\u4e00\u4e2a\u7247\u6bb5\uff0c\u5e76\u5c06\u4e4b\u524d\u7684\u72b6\u6001\u4fdd\u5b58\u4e8e\u8fd4\u56de\u6808\u4e2d\uff1a<\/p>\n<pre><span style=\"color: #993300;\">\/\/ \u521b\u5efa\u65b0\u7684\u7247\u6bb5\u548c\u4e8b\u52a1<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Fragment<\/span> newFragment = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">ExampleFragment<\/span>();<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">FragmentTransaction<\/span> transaction = getFragmentManager().beginTransaction();<\/span>\r\n\r\n<span style=\"color: #993300;\">\/\/ \u7528\u8be5\u7247\u6bb5\u66ff\u6362fragment_container\u89c6\u56fe\u4e2d\u6240\u542b\u6709\u7684\u4efb\u610f\u7247\u6bb5\uff0c<\/span>\r\n<span style=\"color: #993300;\">\/\/ \u5e76\u5c06\u8be5\u4e8b\u52a1\u6dfb\u52a0\u81f3\u8fd4\u56de\u6808<\/span>\r\n<span style=\"color: #000000;\">transaction.replace(R.id.fragment_container, newFragment);<\/span>\r\n<span style=\"color: #000000;\">transaction.addToBackStack(<span style=\"color: #000080;\">null<\/span>);<\/span>\r\n\r\n<span style=\"color: #993300;\">\/\/ \u6267\u884c\u8be5\u4e8b\u52a1<\/span>\r\n<span style=\"color: #000000;\">transaction.commit();<\/span><\/pre>\n<p>\u5728\u8fd9\u4e2a\u8303\u4f8b\u91cc\uff0c<span style=\"color: #008000;\">newFragment<\/span>\u66ff\u4ee3\u4e86\u5f53\u524d\u5728\u7531ID <span style=\"color: #008000;\">R.id.fragment_container<\/span>\u5b9a\u4e49\u7684\u5e03\u5c40\u5bb9\u5668\u4e2d\u7684\uff08\u4efb\u610f\u7684\uff09\u7247\u6bb5\u3002\u901a\u8fc7\u8c03\u7528addToBackStack()\uff0c\u66ff\u6362\u4e8b\u52a1\u88ab\u4fdd\u5b58\u4e8e\u8fd4\u56de\u6808\u4e2d\uff0c\u56e0\u6b64\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6309\u4e0b\u8fd4\u56de\u952e\u6765\u56de\u6eda\u4e8b\u52a1\uff0c\u56de\u5230\u4e4b\u524d\u7684\u7247\u6bb5\u3002<\/p>\n<p>\u5982\u679c\u5c06\u591a\u6b21\u53d8\u66f4\u6dfb\u52a0\u81f3\u4e8b\u52a1\uff08\u4f8b\u5982\u7b2c\u4e8c\u4e2aadd()\u6216\u662fremove()\uff09\u5e76\u8c03\u7528addToBackStack()\u7684\u8bdd\uff0c\u90a3\u4e48\u6240\u6709\u5728\u8c03\u7528commit()\u4e4b\u524d\u88ab\u5e94\u7528\u7684\u53d8\u66f4\u90fd\u5c06\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u4e8b\u52a1\u88ab\u52a0\u5165\u8fd4\u56de\u6808\u4e2d\uff0cBACK\u952e\u5c06\u4f1a\u6062\u590d\u6240\u6709\u8fd9\u4e9b\u53d8\u66f4\u3002<\/p>\n<p>\u5411FragmentTransaction\u4e2d\u6dfb\u52a0\u53d8\u66f4\u7684\u987a\u5e8f\u662f\u65e0\u6240\u8c13\u7684\uff0c\u4e0d\u8fc7\uff1a<\/p>\n<ul>\n<li>\u5fc5\u987b\u5728\u6700\u540e\u8c03\u7528commit()<\/li>\n<li>\u5982\u679c\u5c06\u591a\u4e2a\u7247\u6bb5\u52a0\u5165\u540c\u4e00\u4e2a\u5bb9\u5668\u4e2d\uff0c\u90a3\u4e48\u6dfb\u52a0\u7684\u987a\u5e8f\u5c06\u51b3\u5b9a\u5b83\u4eec\u5728\u89c6\u56fe\u5c42\u7ea7\u4e2d\u7684\u987a\u5e8f<\/li>\n<\/ul>\n<p>\u5982\u679c\u5728\u6267\u884c\u79fb\u9664\u7247\u6bb5\u7684\u4e8b\u52a1\u65f6\u6ca1\u6709\u8c03\u7528addToBackStack()\uff0c\u90a3\u4e48\u8be5\u7247\u6bb5\u5c06\u5728\u4e8b\u52a1\u88ab\u6267\u884c\uff08commit\uff09\u540e\u88ab\u9500\u6bc1\uff0c\u7528\u6237\u65e0\u6cd5\u518d\u6b21\u8fd4\u56de\u5b83\u3002\u53cd\u4e4b\uff0c\u5982\u679c\u5728\u79fb\u9664\u7247\u6bb5\u65f6\u8c03\u7528\u4e86addToBackStack()\uff0c\u90a3\u4e48\u8be5\u7247\u6bb5\u5c06\u88ab\u4e2d\u6b62\uff08stop\uff09\uff0c\u5e76\u5728\u7528\u6237\u8fd4\u56de\u65f6\u88ab\u7ee7\u7eed\uff08resume\uff09\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u63d0\u793a<\/strong>\uff1a\u5bf9\u4e8e\u6bcf\u4e2a\u7247\u6bb5\u4e8b\u52a1\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u5728\u6267\u884c\uff08commit\uff09\u4e4b\u524d\u8c03\u7528setTranstition()\u4ee5\u5e94\u7528\u4e00\u4e2a\u5207\u6362\u52a8\u753b\u3002<\/p>\n<p>\u8c03\u7528commit()\u5e76\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\u4e8b\u52a1\u3002\u5b83\u53ea\u662f\u4f5c\u4e86\u5728\u6d3b\u52a8\u7684UI\u7ebf\u7a0b\uff08\u201c\u4e3b\u201d\u7ebf\u7a0b\uff09\u51c6\u5907\u597d\u4e4b\u65f6\u8fd0\u884c\u8be5\u4e8b\u52a1\u7684\u8c03\u5ea6\u3002\u4e0d\u8fc7\uff0c\u5982\u6709\u5fc5\u8981\uff0c\u53ef\u4ee5\u5728UI\u7ebf\u7a0b\u4e2d\u8c03\u7528executePendingTransactions()\u6765\u7acb\u5373\u6267\u884c\u7531commit()\u63d0\u4ea4\u7684\u4e8b\u52a1\u3002\u53ea\u6709\u5728\u8be5\u4e8b\u52a1\u662f\u5176\u4ed6\u7ebf\u7a0b\u5de5\u4f5c\u7684\u7ec4\u6210\u90e8\u5206\u65f6\u624d\u6709\u5fc5\u8981\u8fd9\u4e48\u505a\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u53ef\u4ee5\u4ec5\u5728\u6d3b\u52a8\u4fdd\u5b58\u5176\u72b6\u6001\u4e4b\u524d\uff08\u5f53\u7528\u6237\u79bb\u5f00\u6d3b\u52a8\u65f6\uff09\u4f7f\u7528commit()\u6765\u6267\u884c\u4e00\u6b21\u4e8b\u52a1\u3002\u5982\u679c\u5728\u6b64\u4e4b\u540e\u6267\u884c\uff0c\u5c06\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002\u8fd9\u662f\u56e0\u4e3a\u5982\u679c\u6d3b\u52a8\u9700\u8981\u88ab\u50a8\u5b58\u7684\u8bdd\uff0c\u6267\u884c\u4e4b\u540e\u7684\u72b6\u6001\u5c06\u65e0\u6cd5\u88ab\u4fdd\u5b58\u3002\u5bf9\u4e8e\u4e22\u5931\u72b6\u6001\u4e5f\u65e0\u59a8\u7684\u60c5\u51b5\uff0c\u5219\u9700\u4f7f\u7528commitAllowingStateLoss()\u3002<\/p>\n<h3><strong>\u548c\u6d3b\u52a8\u8fdb\u884c\u901a\u4fe1<\/strong><\/h3>\n<p>\u5c3d\u7ba1\u4e00\u4e2aFragment\u88ab\u4f5c\u4e3a\u4e00\u4e2a\u72ec\u7acb\u4e8eActivity\u7684\u5bf9\u8c61\u4f7f\u7528\uff0c\u4e14\u53ef\u4ee5\u88ab\u7528\u4e8e\u591a\u4e2a\u6d3b\u52a8\u4e4b\u4e2d\uff0c\u4e00\u4e2a\u7ed9\u5b9a\u7684\u7247\u6bb5\u5b9e\u4f8b\u53ef\u4ee5\u4e0e\u5305\u542b\u5b83\u7684\u6d3b\u52a8\u76f4\u63a5\u5173\u8054\u3002<\/p>\n<p>\u7279\u522b\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7247\u6bb5\u53ef\u4ee5\u4ee5getActivity()\u6765\u83b7\u53d6Activity\u7684\u5b9e\u4f8b\uff0c\u5e76\u80fd\u591f\u5728\u6d3b\u52a8\u7684\u5e03\u5c40\u4e2d\u5f88\u5bb9\u6613\u5730\u8fdb\u884c\u5bfb\u627e\u89c6\u56fe\u4e4b\u7c7b\u7684\u4efb\u52a1\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">View<\/span> listView = <code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Fragment.html#getActivity()\"><span style=\"color: #000000;\">getActivity()<\/span><\/a><\/code>.<code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#findViewById(int)\"><span style=\"color: #000000;\">findViewById<\/span><\/a><\/code>(R.id.list);<\/span><\/pre>\n<p>\u7c7b\u4f3c\u7684\uff0c\u6d3b\u52a8\u53ef\u4ee5\u901a\u8fc7findFragmentById()\u6216findFragmentByTag()\u4eceFragmentManager\u83b7\u53d6\u4e00\u4e2aFragment\u7684\u5f15\u7528\u6765\u8c03\u7528\u7247\u6bb5\u5185\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">ExampleFragment<\/span> fragment = (<span style=\"color: #800080;\">ExampleFragment<\/span>) getFragmentManager().findFragmentById(R.id.example_fragment);<\/span><\/pre>\n<p><strong>\u521b\u5efa\u6d3b\u52a8\u63a5\u53d7\u7684\u4e8b\u4ef6\u56de\u9988<\/strong><\/p>\n<p>\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u9700\u8981\u7247\u6bb5\u4e0e\u6d3b\u52a8\u5171\u4eab\u4e8b\u4ef6\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u7247\u6bb5\u5b9a\u4e49\u4e00\u4e2a\u56de\u9988\u63a5\u53e3\u5e76\u5728\u5bbf\u4e3b\u6d3b\u52a8\u4e2d\u5b9e\u73b0\u5b83\u6765\u5b9e\u73b0\u3002\u5f53\u6d3b\u52a8\u901a\u8fc7\u8be5\u63a5\u53e3\u63a5\u6536\u5230\u4e86\u4e00\u4e2a\u56de\u9988\u65f6\uff0c\u5982\u6709\u9700\u8981\uff0c\u5b83\u53ef\u4ee5\u4e0e\u5e03\u5c40\u4e2d\u7684\u5176\u4ed6\u7247\u6bb5\u5171\u4eab\u4fe1\u606f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\u5728\u4e00\u4e2a\u6d3b\u52a8\u4e2d\u6709\u4e24\u4e2a\u7247\u6bb5\u2014\u2014\u4e00\u4e2a\u7528\u6765\u663e\u793a\u4e00\u5217\u6587\u7ae0\uff08\u7247\u6bb5A\uff09\u800c\u53e6\u4e00\u4e2a\u7528\u6765\u663e\u793a\u67d0\u4e00\u7bc7\u6587\u7ae0\uff08\u7247\u6bb5B\uff09\u2014\u2014\u90a3\u4e48\u7247\u6bb5A\u5fc5\u987b\u5728\u67d0\u4e00\u4e2a\u5217\u8868\u9879\u76ee\u88ab\u9009\u4e2d\u65f6\u544a\u77e5\u6d3b\u52a8\uff0c\u4f7f\u6d3b\u52a8\u80fd\u591f\u544a\u77e5\u7247\u6bb5B\u6765\u663e\u793a\u8be5\u6587\u7ae0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<span style=\"color: #008000;\">OnArticleSelectedListener<\/span>\u63a5\u53e3\u88ab\u58f0\u660e\u4e8e\u7247\u6bb5A\u5185\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">public static class<\/span> <span style=\"color: #800080;\">FragmentA<\/span> <span style=\"color: #000080;\">extends<\/span> <span style=\"color: #800080;\">ListFragment<\/span> {<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 ...<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \/\/ Container Activity must implement this interface<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public interface<\/span> <span style=\"color: #800080;\">OnArticleSelectedListener<\/span> {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 public void<\/span> onArticleSelected(<span style=\"color: #800080;\">Uri<\/span> articleUri);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 ...<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u4e4b\u540e\u6301\u6709\u8be5\u7247\u6bb5\u7684\u6d3b\u52a8\u5b9e\u73b0\u4e86<span style=\"color: #008000;\">OnArticleSelectedListener<\/span>\u63a5\u53e3\u5e76\u8986\u76d6\u4e86<span style=\"color: #008000;\">onArticleSelected<\/span>()\u6765\u5c06\u6765\u81ea\u4e8e\u7247\u6bb5A\u7684\u4e8b\u4ef6\u901a\u77e5\u7ed9\u7247\u6bb5B\u3002\u4e3a\u786e\u4fdd\u5bbf\u4e3b\u6d3b\u52a8\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\uff0c\u7247\u6bb5A\u7684onAttach()\u56de\u9988\u65b9\u6cd5\uff08\u5f53\u628a\u7247\u6bb5\u6dfb\u52a0\u81f3\u6d3b\u52a8\u65f6\u7cfb\u7edf\u5c06\u4f1a\u81ea\u52a8\u8c03\u7528\u8be5\u65b9\u6cd5\uff09\u901a\u8fc7\u5c06\u4f20\u9012\u8fc7\u6765\u7684Activity\u5f3a\u5236\u8f6c\u6362\u4e3a\u4e86onAttach()\u4ee5\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a<span style=\"color: #008000;\">OnArticleSelectedListener<\/span>\u7684\u5b9e\u4f8b\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">public static class<\/span> <span style=\"color: #800080;\">FragmentA<\/span> <span style=\"color: #000080;\">extends<\/span> <span style=\"color: #800080;\">ListFragment<\/span> {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 OnArticleSelectedListener<\/span> mListener;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 ...<\/span>\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public void<\/span> onAttach(<span style=\"color: #800080;\">Activity<\/span> activity) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 super<\/span>.onAttach(activity);<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 try<\/span> {<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 mListener = (<span style=\"color: #800080;\">OnArticleSelectedListener<\/span>) activity;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 } <span style=\"color: #000080;\">catch<\/span> (<span style=\"color: #800080;\">ClassCastException<\/span> e) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 throw new<\/span> <span style=\"color: #800080;\">ClassCastException<\/span>(activity.toString() + <span style=\"color: #008000;\">\" must implement OnArticleSelectedListener\"<\/span>);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 ...<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u5982\u679c\u8be5\u6d3b\u52a8\u6ca1\u6709\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\uff0c\u90a3\u4e48\u7247\u6bb5\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2aClassCastException\u3002\u4e00\u65e6\u6210\u529f\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\uff0c<span style=\"color: #008000;\">mListener<\/span>\u6210\u5458\u5c06\u6301\u6709\u4e00\u4e2a\u6d3b\u52a8\u7684<span style=\"color: #008000;\">OnArticleSelectedListener<\/span>\u5b9e\u73b0\u7684\u5f15\u7528\uff0c\u56e0\u6b64\u7247\u6bb5A\u5c06\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7531<span style=\"color: #008000;\">OnArticleSelectedListener<\/span>\u63a5\u53e3\u5b9a\u4e49\u7684\u65b9\u6cd5\u548c\u6d3b\u52a8\u5171\u4eab\u4e8b\u4ef6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7247\u6bb5A\u662fListFragment\u7684\u4e00\u4e2a\u7ee7\u627f\uff0c\u90a3\u4e48\u6bcf\u6b21\u7528\u6237\u70b9\u51fb\u4e00\u4e2a\u5217\u8868\u9879\u76ee\u65f6\uff0c\u7cfb\u7edf\u5c06\u8c03\u7528\u7247\u6bb5\u7684onListItemClick()\uff0c\u5b83\u5c06\u4e4b\u540e\u8c03\u7528<span style=\"color: #008000;\">onArticleSelected<\/span>()\u6765\u4e0e\u6d3b\u52a8\u5171\u4eab\u8be5\u4e8b\u4ef6\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">public static class<\/span> <span style=\"color: #800080;\">FragmentA<\/span> <span style=\"color: #000080;\">extends<\/span> <span style=\"color: #800080;\">ListFragment<\/span> {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 OnArticleSelectedListener<\/span> mListener;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 ...<\/span>\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public void<\/span> onListItemClick(<span style=\"color: #800080;\">ListView<\/span> l, <span style=\"color: #800080;\">View<\/span> v, <span style=\"color: #000080;\">int<\/span> position, <span style=\"color: #000080;\">long<\/span> id) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ Append the clicked item's row ID with the content provider Uri<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 Uri<\/span> noteUri = <span style=\"color: #800080;\">ContentUris<\/span>.<code><a href=\"http:\/\/developer.android.com\/reference\/android\/content\/ContentUris.html#withAppendedId(android.net.Uri, long)\"><span style=\"color: #000000;\">withAppendedId<\/span><\/a><\/code>(<span style=\"color: #800080;\">ArticleColumns<\/span>.CONTENT_URI, id);<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ Send the event and Uri to the host activity<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 mListener.onArticleSelected(noteUri);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 ...<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u4f20\u9012\u7ed9onListItemClick()\u7684id\u53c2\u6570\u662f\u88ab\u70b9\u51fb\u9879\u76ee\u7684\u884cID\uff0c\u5b83\u88ab\u6d3b\u52a8\uff08\u6216\u5176\u4ed6\u7247\u6bb5\uff09\u7528\u6765\u4ece\u5e94\u7528\u7a0b\u5e8f\u7684ContentProvider\u4e2d\u83b7\u53d6\u6587\u7ae0\u3002<\/p>\n<p>\u5173\u4e8e\u4f7f\u7528\u5185\u5bb9\u63d0\u4f9b\u5668\u7684\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u89c1\u201cContent Providers\u6587\u6863\u201d\u3002<\/p>\n<p><strong>\u6dfb\u52a0\u9879\u76ee\u81f3\u52a8\u4f5c\u6761\uff08Action Bar\uff09<\/strong><\/p>\n<p>\u7247\u6bb5\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0onCreateOptionsMenu()\u65b9\u6cd5\u6765\u548c\u6d3b\u52a8\u7684\u9009\u9879\u83dc\u5355\uff08Options Menu\uff09\u9879\u76ee\u76f8\u7ed3\u5408\u4f7f\u7528\uff08\u56e0\u6b64\u4e5f\u5c31\u53ef\u4ee5\u548c\u52a8\u4f5c\u6761\u4e00\u8d77\u4f7f\u7528\uff09\u3002\u4e0d\u8fc7\uff0c\u4e3a\u4e86\u4f7f\u8be5\u65b9\u6cd5\u80fd\u591f\u63a5\u53d7\u8c03\u7528\uff0c\u5fc5\u987b\u5728onCreate()\u4e2d\u8c03\u7528setHasOptionsMenu()\u6765\u6807\u8bc6\u8be5\u7247\u6bb5\u5c06\u88ab\u6dfb\u52a0\u81f3\u9009\u9879\u83dc\u5355\uff08\u5426\u5219\uff0c\u8be5\u7247\u6bb5\u5c06\u4e0d\u4f1a\u63a5\u6536\u5230\u5bf9onCreateOptionsMenu()\u7684\u8c03\u7528\uff09<\/p>\n<p>\u4e4b\u540e\u4ece\u7247\u6bb5\u6dfb\u52a0\u81f3\u9009\u9879\u83dc\u5355\u7684\u4efb\u4f55\u9879\u76ee\u90fd\u5c06\u88ab\u589e\u52a0\u5230\u5df2\u6709\u83dc\u5355\u9879\u76ee\u4e4b\u540e\u3002\u8be5\u7247\u6bb5\u8fd8\u5c06\u5728\u67d0\u4e00\u83dc\u5355\u9879\u76ee\u88ab\u9009\u4e2d\u65f6\u6536\u5230onOptionsItemSelected()\u7684\u56de\u9988\u3002<\/p>\n<p>\u4e5f\u53ef\u4ee5\u5728\u7247\u6bb5\u5e03\u5c40\u4e2d\u6ce8\u518c\u4e00\u4e2a\u89c6\u56fe\uff0c\u901a\u8fc7\u8c03\u7528registerForContextMenu()\u6765\u63d0\u4f9b\u4e0a\u4e0b\u6587\u83dc\u5355\u3002\u5f53\u7528\u6237\u6253\u5f00\u4e0a\u4e0b\u6587\u83dc\u5355\u65f6\uff0c\u8be5\u7247\u6bb5\u5c06\u6536\u5230\u4e00\u4e2a\u5bf9onCreateContextMenu()\u7684\u8c03\u7528\u3002\u5f53\u7528\u6237\u9009\u4e2d\u4e00\u4e2a\u9879\u76ee\u65f6\uff0c\u7247\u6bb5\u5c06\u6536\u5230\u4e00\u4e2a\u5bf9onContextItemSelected()\u7684\u8c03\u7528\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u5c3d\u7ba1\u7247\u6bb5\u5bf9\u6bcf\u4e00\u4e2a\u6dfb\u52a0\u8fc7\u7684\u9879\u76ee\u90fd\u80fd\u6536\u5230\u4e00\u4e2a\u9879\u76ee\u9009\u4e2d\u56de\u9988\uff0c\u4f46\u6d3b\u52a8\u672c\u8eab\u5c06\u5728\u7528\u6237\u9009\u62e9\u83dc\u5355\u9879\u76ee\u65f6\u9996\u5148\u6536\u5230\u76f8\u5e94\u7684\u56de\u9988\u3002\u5982\u679c\u6d3b\u52a8\u7684\u9879\u76ee\u9009\u4e2d\u56de\u9988\u7684\u5b9e\u73b0\u6ca1\u6709\u5904\u7406\u8be5\u9009\u4e2d\u7684\u9879\u76ee\uff0c\u90a3\u4e48\u8be5\u4e8b\u4ef6\u624d\u4f1a\u88ab\u4f20\u9012\u7ed9\u7247\u6bb5\u7684\u56de\u9988\u65b9\u6cd5\u3002\u5bf9\u4e8e\u9009\u9879\u83dc\u5355\u548c\u4e0a\u4e0b\u6587\u83dc\u5355\u6765\u8bf4\u90fd\u662f\u5982\u6b64\u3002<\/p>\n<p>\u5173\u4e8e\u83dc\u5355\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1\u201c\u83dc\u5355\u201d\u548c\u201c\u52a8\u4f5c\u6761\u201d\u5f00\u53d1\u8005\u6307\u5357\u3002<\/p>\n<h3><strong>\u5904\u7406\u7247\u6bb5\u751f\u547d\u5468\u671f<\/strong><\/h3>\n<p>\u7ba1\u7406\u4e00\u4e2a\u7247\u6bb5\u7684\u751f\u547d\u5468\u671f\u548c\u7ba1\u7406\u4e00\u4e2a\u6d3b\u52a8\u7684\u751f\u547d\u5468\u671f\u5341\u5206\u7684\u76f8\u50cf\u3002\u5982\u540c\u6d3b\u52a8\u4e00\u6837\uff0c\u4e00\u4e2a\u7247\u6bb5\u53ef\u4ee5\u4ee5\u4e09\u79cd\u72b6\u6001\u5b58\u5728\uff1a<\/p>\n<p><em>Resumed<\/em><\/p>\n<p style=\"padding-left: 30px;\">\u8be5\u7247\u6bb5\u5728\u8fd0\u884c\u4e2d\u7684\u6d3b\u52a8\u91cc\u662f\u53ef\u89c1\u7684\u3002<\/p>\n<p><em>Paused<\/em><\/p>\n<p style=\"padding-left: 30px;\">\u53e6\u4e00\u4e2a\u6d3b\u52a8\u6b63\u5904\u4e8e\u524d\u53f0\u4e14\u83b7\u5f97\u4e86\u7126\u70b9\uff0c\u4e0d\u8fc7\u542b\u6709\u8be5\u7247\u6bb5\u7684\u6d3b\u52a8\u4f9d\u7136\u662f\u53ef\u89c1\u7684\uff08\u524d\u53f0\u7684\u6d3b\u52a8\u662f\u534a\u900f\u660e\u7684\u6216\u6ca1\u6709\u8986\u76d6\u6574\u4e2a\u5c4f\u5e55 \uff09\u3002<\/p>\n<p><em>Stopped<\/em><\/p>\n<p style=\"padding-left: 30px;\">\u7247\u6bb5\u4e0d\u53ef\u89c1\u3002\u6216\u662f\u5bbf\u4e3b\u6d3b\u52a8\u88ab\u505c\u6b62\u4e86\uff0c\u6216\u662f\u7247\u6bb5\u88ab\u4ece\u6d3b\u52a8\u4e2d\u79fb\u9664\u540e\u52a0\u5165\u4e86\u8fd4\u56de\u6808\u3002\u4e00\u4e2a\u88ab\u505c\u6b62\u7684\u7247\u6bb5\u4f9d\u7136\u662f\u5b58\u5728\u7740\u7684\uff08\u6240\u6709\u7684\u72b6\u6001\u548c\u6210\u5458\u4fe1\u606f\u7531\u7cfb\u7edf\u4fdd\u6301\u7740\uff09\u3002\u4e0d\u8fc7\uff0c\u5b83\u5bf9\u4e8e\u7528\u6237\u4e0d\u518d\u53ef\u89c1\uff0c\u5728\u6d3b\u52a8\u88ab\u6740\u9664\u540e\u4e5f\u5c06\u88ab\u6740\u9664\u3002<\/p>\n<p>\u548c\u4e00\u4e2a\u6d3b\u52a8\u4e00\u6837\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2aBundle\u4ee5\u5728\u6d3b\u52a8\u7684\u8fdb\u7a0b\u88ab\u6740\u9664\u800c\u53c8\u9700\u8981\u5728\u8be5\u6d3b\u52a8\u88ab\u91cd\u5efa\u65f6\u8fd8\u539f\u8be5\u7247\u6bb5\u7684\u60c5\u51b5\u4e0b\u4fdd\u5b58\u7247\u6bb5\u7684\u72b6\u6001\u3002\u53ef\u4ee5\u5728\u6d3b\u52a8\u7684onSaveInstanceState()\u56de\u9988\u65b9\u6cd5\u4e2d\u4fdd\u5b58\u72b6\u6001\uff0c\u5728onCreate()\u3001onCreateView()\u6216onActivityCreated()\u4e2d\u8fd8\u539f\u72b6\u6001\u3002\u5173\u4e8e\u4fdd\u5b58\u72b6\u6001\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1\u201c\u6d3b\u52a8\u201d\u6587\u6863\u3002<\/p>\n<figure id=\"attachment_986\" aria-describedby=\"caption-attachment-986\" style=\"width: 403px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/09\/activity_fragment_lifecycle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-986\" title=\"activity_fragment_lifecycle\" src=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/09\/activity_fragment_lifecycle.png\" alt=\"\" width=\"403\" height=\"691\" \/><\/a><figcaption id=\"caption-attachment-986\" class=\"wp-caption-text\">\u56fe 3. \u6d3b\u52a8\u751f\u547d\u5468\u671f\u5bf9\u4e8e\u7247\u6bb5\u751f\u547d\u5468\u671f\u7684\u5f71\u54cd<\/figcaption><\/figure>\n<p>\u4e00\u4e2a\u6d3b\u52a8\u548c\u4e00\u4e2a\u7247\u6bb5\u7684\u751f\u547d\u5468\u671f\u4e4b\u95f4\u6700\u5927\u7684\u533a\u522b\u5728\u4e8e\u5b83\u4eec\u662f\u5982\u4f55\u88ab\u4fdd\u5b58\u4e8e\u5404\u81ea\u7684\u8fd4\u56de\u6808\u4e2d\u7684\u3002\u4e00\u4e2a\u6d3b\u52a8\u9ed8\u8ba4\u4e3a\u5728\u5176\u88ab\u505c\u6b62\u65f6\u88ab\u653e\u5165\u7531\u7cfb\u7edf\u6240\u7ba1\u7406\u7684\u4e00\u4e2a\u8fd4\u56de\u6808\u4e2d\uff08\u8fd9\u6837\u7528\u6237\u5c31\u80fd\u901a\u8fc7BACK\u952e\u8fd4\u56de\u8be5\u6d3b\u52a8\uff0c\u5c31\u50cf\u201c\u4efb\u52a1\u548c\u8fd4\u56de\u6808\u201d\u4e2d\u6240\u8bf4\u7684\u90a3\u6837\uff09\u3002\u7136\u800c\uff0c \u4e00\u4e2a\u7247\u6bb5\u53ea\u6709\u5728\u8fdb\u884c\u7247\u6bb5\u79fb\u9664\u7684\u4e8b\u52a1\u4e2d\u8c03\u7528addToBackStack()\u6765\u663e\u5f0f\u5730\u8bf7\u6c42\u4fdd\u5b58\u7247\u6bb5\u7684\u5b9e\u4f8b\u65f6\u624d\u4f1a\u88ab\u653e\u5165\u7531\u5bbf\u4e3b\u6d3b\u52a8\u6240\u7ba1\u7406\u7684\u8fd4\u56de\u6808\u4e2d\u3002<\/p>\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u7ba1\u7406\u7247\u6bb5\u7684\u751f\u547d\u5468\u671f\u548c\u7ba1\u7406\u6d3b\u52a8\u7684\u751f\u547d\u5468\u671f\u5341\u5206\u76f8\u4f3c\u3002\u56e0\u6b64\uff0c\u7ba1\u7406\u6d3b\u52a8\u751f\u547d\u5468\u671f\u65f6\u7684\u505a\u6cd5\u540c\u6837\u53ef\u4ee5\u5e94\u7528\u4e8e\u7ba1\u7406\u7247\u6bb5\u3002\u6240\u9700\u8981\u7406\u89e3\u7684\u5c31\u4ec5\u4ec5\u662f\u6d3b\u52a8\u7684\u751f\u547d\u5468\u671f\u5c06\u4f1a\u5730\u7247\u6bb5\u7684\u751f\u547d\u5468\u671f\u6709\u7740\u600e\u6837\u7684\u5f71\u54cd\u3002<\/p>\n<h3><strong>\u8303\u4f8b<\/strong><\/h3>\n<p>\u4e3a\u4e86\u603b\u7ed3\u672c\u6587\u6863\u4e2d\u6240\u8bf4\u7684\uff0c\u4e0b\u9762\u6709\u4e00\u4e2a\u8303\u4f8b\u6765\u6f14\u793a\u4e00\u4e2a\u4f7f\u7528\u4e86\u4e24\u4e2a\u7247\u6bb5\u7684\u6d3b\u52a8\u662f\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u53cc\u9762\u677f\u5e03\u5c40\u7684\u3002\u4e0b\u9762\u7684\u8fd9\u4e2a\u6d3b\u52a8\u5305\u542b\u4e86\u4e00\u4e2a\u663e\u793a\u4e00\u5217\u838e\u58eb\u6bd4\u4e9a\u620f\u5267\u6807\u9898\u7684\u7247\u6bb5\u548c\u53e6\u4e00\u4e2a\u5728\u4ece\u5217\u8868\u4e2d\u9009\u62e9\u9879\u76ee\u540e\u663e\u793a\u8be5\u620f\u5267\u7684\u6458\u8981\u7684\u7247\u6bb5\u3002\u5b83\u8fd8\u6f14\u793a\u4e86\u5982\u4f55\u57fa\u4e8e\u4e0d\u540c\u7684\u5c4f\u5e55\u914d\u7f6e\u6765\u63d0\u4f9b\u4e0d\u540c\u7684\u7247\u6bb5\u914d\u7f6e\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f\uff1a<\/strong>\u8be5\u6d3b\u52a8\u7684\u5b8c\u6574\u6e90\u4ee3\u7801\u53ef\u4ee5\u5728FragmentLayout.java\u4e2d\u5f97\u5230\u3002<\/p>\n<p>\u4e3b\u6d3b\u52a8\u6309\u7167\u5e38\u89c4\u7684\u65b9\u5f0f\u5728onCreate()\u4e2d\u5e94\u7528\u5e03\u5c40\uff1a<\/p>\n<pre><span style=\"color: #008080;\">@Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">protected void<\/span> onCreate(<span style=\"color: #800080;\">Bundle<\/span> savedInstanceState) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 super<\/span>.onCreate(savedInstanceState);<\/span>\r\n\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 setContentView(R.layout.fragment_layout);<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u88ab\u5e94\u7528\u7684\u5e03\u5c40\u662f<span style=\"color: #008000;\">fragment_layout.xml<\/span>\uff1a<\/p>\n<pre><span style=\"color: #800080;\"><span style=\"color: #000080;\">&lt;LinearLayout<\/span> xmlns:android=<span style=\"color: #008000;\">\"http:\/\/schemas.android.com\/apk\/res\/android\"<\/span><\/span>\r\n<span style=\"color: #800080;\">\u00a0 \u00a0 android:orientation=<span style=\"color: #008000;\">\"horizontal\"<\/span><\/span>\r\n<span style=\"color: #800080;\">\u00a0 \u00a0 android:layout_width=<span style=\"color: #008000;\">\"match_parent\"<\/span> android:layout_height=<span style=\"color: #008000;\">\"match_parent\"<\/span><span style=\"color: #000080;\">&gt;<\/span><\/span>\r\n\r\n<span style=\"color: #800080;\"><span style=\"color: #000080;\">\u00a0 \u00a0 &lt;fragment<\/span> class=<span style=\"color: #008000;\">\"com.example.android.apis.app.FragmentLayout$TitlesFragment\"<\/span><\/span>\r\n<span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:id=<span style=\"color: #008000;\">\"@+id\/titles\"<\/span> android:layout_weight=<span style=\"color: #008000;\">\"1\"<\/span><\/span>\r\n<span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_width=<span style=\"color: #008000;\">\"0px\"<\/span> android:layout_height=<span style=\"color: #008000;\">\"match_parent\"<\/span> <span style=\"color: #000080;\">\/&gt;<\/span><\/span>\r\n\r\n<span style=\"color: #800080;\"><span style=\"color: #000080;\">\u00a0 \u00a0 &lt;FrameLayout<\/span> android:id=<span style=\"color: #008000;\">\"@+id\/details\"<\/span> android:layout_weight=<span style=\"color: #008000;\">\"1\"<\/span><\/span>\r\n<span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_width=<span style=\"color: #008000;\">\"0px\"<\/span> android:layout_height=<span style=\"color: #008000;\">\"match_parent\"<\/span><\/span>\r\n<span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:background=<span style=\"color: #008000;\">\"?android:attr\/detailsElementBackground\"<\/span> <span style=\"color: #000080;\">\/&gt;<\/span><\/span>\r\n\r\n<span style=\"color: #000080;\">&lt;\/LinearLayout&gt;<\/span><\/pre>\n<p>\u4f7f\u7528\u8be5\u5e03\u5c40\uff0c\u7cfb\u7edf\u5c06\u5728\u6d3b\u52a8\u8f7d\u5165\u8be5\u5e03\u5c40\u65f6\u5b9e\u4f8b\u5316<span style=\"color: #008000;\">TitlesFragment<\/span>\uff08\u5b83\u5217\u51fa\u4e86\u620f\u5267\u6807\u9898\uff09\uff0c\u800cFrameLayout\uff08\u5b83\u663e\u793a\u620f\u5267\u7684\u6458\u8981\uff09\u5c06\u5360\u7528\u5c4f\u5e55\u7684\u53f3\u4fa7\uff08\u4e0d\u8fc7\u6700\u521d\u5b83\u662f\u7a7a\u7740\u7684\uff09\u3002\u53ef\u4ee5\u53d1\u73b0\uff0c\u76f4\u5230\u7528\u6237\u4ece\u5217\u8868\u4e2d\u9009\u62e9\u4e86\u4e00\u4e2a\u9879\u76ee\u4e4b\u540e\u67d0\u4e00\u4e2a\u7247\u6bb5\u624d\u4f1a\u88ab\u653e\u5165FrameLayout\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u4e0d\u8fc7\u6240\u6709\u7684\u5c4f\u5e55\u914d\u7f6e\u90fd\u8db3\u591f\u5bbd\u6765\u5728\u4e24\u8fb9\u663e\u793a\u620f\u5267\u5217\u8868\u548c\u6458\u8981\u3002\u6240\u4ee5\u4e0a\u9762\u7684\u5e03\u5c40\u4ec5\u7528\u4e8e\u6a2a\u5c4f\u6a21\u5f0f\uff0c\u9700\u8981\u4fdd\u5b58\u4e3a<span style=\"color: #008000;\">res\/layout-land\/fragment_layout.xml<\/span>\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5f53\u5c4f\u5e55\u662f\u7ad6\u5411\u65f6\uff0c\u7cfb\u7edf\u5c06\u5e94\u7528\u4e0b\u9762\u7684\u4fdd\u5b58\u4e8e<span style=\"color: #008000;\">res\/layout\/fragment_layout.xml<\/span>\u7684\u5e03\u5c40\uff1a<\/p>\n<pre><span style=\"color: #800080;\"><span style=\"color: #000080;\">&lt;FrameLayout<\/span> xmlns:android=<span style=\"color: #008000;\">\"http:\/\/schemas.android.com\/apk\/res\/android\"<\/span><\/span>\r\n<span style=\"color: #800080;\">\u00a0 \u00a0 android:layout_width=<span style=\"color: #008000;\">\"match_parent\"<\/span> android:layout_height=<span style=\"color: #008000;\">\"match_parent\"<\/span><span style=\"color: #000080;\">&gt;<\/span><\/span>\r\n<span style=\"color: #800080;\"><span style=\"color: #000080;\">\u00a0 \u00a0 &lt;fragment<\/span> class=<span style=\"color: #008000;\">\"com.example.android.apis.app.FragmentLayout$TitlesFragment\"<\/span><\/span>\r\n<span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:id=<span style=\"color: #008000;\">\"@+id\/titles\"<\/span><\/span>\r\n<span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_width=<span style=\"color: #008000;\">\"match_parent\"<\/span> android:layout_height=<span style=\"color: #008000;\">\"match_parent\"<\/span> <span style=\"color: #000080;\">\/&gt;<\/span><\/span>\r\n<span style=\"color: #000080;\">&lt;\/FrameLayout&gt;<\/span><\/pre>\n<p>\u8be5\u5e03\u5c40\u4ec5\u5305\u542b\u4e86<span style=\"color: #008000;\">TitlesFragment<\/span>\u3002\u8fd9\u610f\u5473\u7740\u5f53\u8bbe\u5907\u5904\u4e8e\u7ad6\u5411\u72b6\u6001\u65f6\uff0c\u53ea\u6709\u620f\u5267\u6807\u9898\u5217\u8868\u662f\u53ef\u89c1\u7684\u3002\u56e0\u6b64\uff0c\u5f53\u7528\u6237\u5728\u8fd9\u65f6\u70b9\u51fb\u4e86\u5217\u8868\u9879\u76ee\u7684\u8bdd\uff0c\u7a0b\u5e8f\u4f1a\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u6d3b\u52a8\u6765\u663e\u793a\u6458\u8981\uff0c\u800c\u4e0d\u662f\u8f7d\u5165\u7b2c\u4e8c\u4e2a\u7247\u6bb5\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u770b\u4e00\u4e0b\u5982\u4f55\u5728\u7247\u6bb5\u7c7b\u4e2d\u5b8c\u6210\u6574\u4e2a\u8fc7\u7a0b\u3002\u9996\u5148\u662f<span style=\"color: #008000;\">TitlesFragment<\/span>\uff0c\u5b83\u663e\u793a\u4e86\u4e00\u5217\u838e\u58eb\u6bd4\u4e9a\u620f\u5267\u6807\u9898\u3002\u8be5\u7247\u6bb5\u7ee7\u627f\u4e8eListFragment\uff0c\u901a\u8fc7\u5b83\u6765\u5904\u7406\u5927\u90e8\u5206\u7684\u5217\u8868\u89c6\u56fe\u5de5\u4f5c\u3002<\/p>\n<p>\u6b63\u5982\u4ee3\u7801\u4e2d\u6240\u8868\u660e\u7684\uff0c\u9700\u8981\u6ce8\u610f\u5f53\u7528\u6237\u70b9\u51fb\u4e00\u4e2a\u5217\u8868\u9879\u76ee\u65f6\u6709\u4e24\u79cd\u53ef\u80fd\u7684\u884c\u4e3a\uff1a\u8fd9\u53d6\u51b3\u4e8e\u4e24\u79cd\u5e03\u5c40\u4e2d\u7684\u54ea\u4e00\u79cd\u4f7f\u5904\u4e8e\u6fc0\u6d3b\u72b6\u6001\u7684\u3002\u5b83\u65e2\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u6d3b\u52a8\u4e2d\u521b\u5efa\u5e76\u8868\u793a\u4e00\u4e2a\u65b0\u7684\u7247\u6bb5\u6765\u663e\u793a\u8be6\u7ec6\u4fe1\u606f\uff08\u5c06\u7247\u6bb5\u52a0\u5165FrameLayout\uff09\uff0c\u4e5f\u53ef\u4ee5\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u6d3b\u52a8\uff08\u5e76\u5728\u90a3\u91cc\u663e\u793a\u7247\u6bb5\uff09\u3002<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">public static<\/span> <span style=\"color: #000080;\">class<\/span> <span style=\"color: #800080;\">TitlesFragment<\/span> <span style=\"color: #000080;\">extends<\/span> <span style=\"color: #800080;\">ListFragment<\/span> {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 boolean<\/span> mDualPane;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 int<\/span> mCurCheckPosition = 0;<\/span>\r\n\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public void<\/span> onActivityCreated(<span style=\"color: #800080;\">Bundle<\/span> savedInstanceState) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 super<\/span>.onActivityCreated(savedInstanceState);<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ Populate list with our static array of titles.<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 setListAdapter(<span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">ArrayAdapter<\/span>&lt;<span style=\"color: #800080;\">String<\/span>&gt;(getActivity(),<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android.R.layout.simple_list_item_activated_1, <span style=\"color: #800080;\">Shakespeare<\/span>.TITLES));<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ Check to see if we have a frame in which to embed the details<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ fragment directly in the containing UI.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 View<\/span> detailsFrame = getActivity().findViewById(R.id.details);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 mDualPane = detailsFrame != <span style=\"color: #000080;\">null<\/span> &amp;&amp; detailsFrame.getVisibility() == <span style=\"color: #800080;\">View<\/span>.VISIBLE;<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 if<\/span> (savedInstanceState != <span style=\"color: #000080;\">null<\/span>) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Restore last state for checked position.<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 mCurCheckPosition = savedInstanceState.getInt(<span style=\"color: #008000;\">\"curChoice\"<\/span>, 0);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 if<\/span> (mDualPane) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ In dual-pane mode, the list view highlights the selected item.<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 getListView().setChoiceMode(<span style=\"color: #800080;\">ListView<\/span>.CHOICE_MODE_SINGLE);<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Make sure our UI is in the correct state.<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 showDetails(mCurCheckPosition);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public void<\/span> onSaveInstanceState(<span style=\"color: #800080;\">Bundle<\/span> outState) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 super<\/span>.onSaveInstanceState(outState);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 outState.putInt(<span style=\"color: #008000;\">\"curChoice\"<\/span>, mCurCheckPosition);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public void<\/span> onListItemClick(<span style=\"color: #800080;\">ListView<\/span> l, <span style=\"color: #800080;\">View<\/span> v, <span style=\"color: #000080;\">int<\/span> position, <span style=\"color: #000080;\">long<\/span> id) {<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 showDetails(position);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \/**<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0* Helper function to show the details of a selected item, either by<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0* displaying a fragment in-place in the current UI, or starting a<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0* whole new activity in which it is displayed.<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0*\/<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 void<\/span> showDetails(<span style=\"color: #000080;\">int<\/span> index) {<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 mCurCheckPosition = index;<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 if<\/span> (mDualPane) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ We can display everything in-place with fragments, so update<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ the list to highlight the selected item and show the data.<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 getListView().setItemChecked(index, <span style=\"color: #000080;\">true<\/span>);<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Check what fragment is currently shown, replace if needed.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 DetailsFragment<\/span> details = (<span style=\"color: #800080;\">DetailsFragment<\/span>)<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 getFragmentManager().findFragmentById(R.id.details);<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if<\/span> (details == <span style=\"color: #000080;\">null<\/span> || details.getShownIndex() != index) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Make new fragment to show this selection.<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 details = <span style=\"color: #800080;\">DetailsFragment<\/span>.newInstance(index);<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Execute a transaction, replacing any existing fragment<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ with this one inside the frame.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FragmentTransaction<\/span> ft = getFragmentManager().beginTransaction();<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ft.replace(R.id.details, details);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ft.setTransition(<span style=\"color: #800080;\">FragmentTransaction<\/span>.TRANSIT_FRAGMENT_FADE);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ft.commit();<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 } <span style=\"color: #000080;\">else<\/span> {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Otherwise we need to launch a new activity to display<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ the dialog fragment with selected text.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Intent<\/span> intent = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">Intent<\/span>();<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 intent.setClass(getActivity(), <span style=\"color: #800080;\">DetailsActivity<\/span>.<span style=\"color: #000080;\">class<\/span>);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 intent.putExtra(<span style=\"color: #008000;\">\"index\"<\/span>, index);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 startActivity(intent);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u7b2c\u4e8c\u4e2a\u7247\u6bb5\uff0c<span style=\"color: #008000;\">DetailsFragment<\/span>\u663e\u793a\u4e86\u4ece<span style=\"color: #008000;\">TitlesFragment<\/span>\u5217\u8868\u4e2d\u6240\u9009\u62e9\u7684\u620f\u5267\u9879\u76ee\u7684\u6458\u8981\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">public static class<\/span> <span style=\"color: #800080;\">DetailsFragment<\/span> <span style=\"color: #000080;\">extends<\/span> <span style=\"color: #800080;\">Fragment<\/span> {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \/**<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0* Create a new instance of DetailsFragment, initialized to<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0* show the text at 'index'.<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0*\/<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public static<\/span> <span style=\"color: #800080;\">DetailsFragment<\/span> newInstance(<span style=\"color: #000080;\">int<\/span> index) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 DetailsFragment<\/span> f = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">DetailsFragment<\/span>();<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ Supply index input as an argument.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 Bundle<\/span> args = new <span style=\"color: #800080;\">Bundle<\/span>();<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 args.putInt(<span style=\"color: #008000;\">\"index\"<\/span>, index);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 f.setArguments(args);<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 return<\/span> f;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public int<\/span> getShownIndex() {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 return<\/span> getArguments().getInt(<span style=\"color: #008000;\">\"index\"<\/span>, <span style=\"color: #008080;\">0<\/span>);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public<\/span> <span style=\"color: #800080;\">View<\/span> onCreateView(<span style=\"color: #800080;\">LayoutInflater<\/span> inflater, <span style=\"color: #800080;\">ViewGroup<\/span> container,<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Bundle<\/span> savedInstanceState) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 if<\/span> (container == <span style=\"color: #000080;\">null<\/span>) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ We have different layouts, and in one of them this<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ fragment's containing frame doesn't exist. \u00a0The fragment<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ may still be created from its saved state, but there is<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ no reason to try to create its view hierarchy because it<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ won't be displayed. \u00a0Note this is not needed -- we could<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ just run the code below, where we would create and return<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ the view hierarchy; it would just never be used.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return null<\/span>;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 ScrollView<\/span> scroller = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">ScrollView<\/span>(getActivity());<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 TextView<\/span> text = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">TextView<\/span>(getActivity());<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 int<\/span> padding = (<span style=\"color: #000080;\">int<\/span>)<span style=\"color: #800080;\">TypedValue<\/span>.applyDimension(<span style=\"color: #800080;\">TypedValue<\/span>.COMPLEX_UNIT_DIP,<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #008080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 4<\/span>, getActivity().getResources().getDisplayMetrics());<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 text.setPadding(padding, padding, padding, padding);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 scroller.addView(text);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 text.setText(<span style=\"color: #800080;\">Shakespeare<\/span>.DIALOGUE[getShownIndex()]);<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 return<\/span> scroller;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u56de\u5fc6\u4e00\u4e0b<span style=\"color: #008000;\">TitlesFragment<\/span>\u7c7b\uff0c\u5982\u679c\u7528\u6237\u70b9\u51fb\u4e86\u4e00\u4e2a\u5217\u8868\u9879\u76ee\u65f6\u5f53\u524d\u5e03\u5c40\u6ca1\u6709\u5305\u542b<span style=\"color: #008000;\">R.id.details<\/span>\u89c6\u56fe\u7684\u8bdd\uff08<span style=\"color: #008000;\">DetailsFragment<\/span>\u5c5e\u4e8e\u8be5\u89c6\u56fe\uff09\uff0c\u5e94\u7528\u7a0b\u5e8f\u5c06\u542f\u52a8<span style=\"color: #008000;\">DetailsActivity<\/span>\u6d3b\u52a8\u6765\u663e\u793a\u9879\u76ee\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u4e0b\u9762\u662f<span style=\"color: #008000;\">DetailsActivity<\/span>\uff0c\u5b83\u4ec5\u4ec5\u5185\u5d4c\u4e86<span style=\"color: #008000;\">DetailsFragment<\/span>\u4ee5\u5728\u5c4f\u5e55\u5904\u4e8e\u7ad6\u5c4f\u6a21\u5f0f\u65f6\u663e\u793a\u6240\u9009\u62e9\u7684\u620f\u5267\u6458\u8981\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">public static class<\/span> <span style=\"color: #800080;\">DetailsActivity<\/span> <span style=\"color: #000080;\">extends<\/span> <span style=\"color: #800080;\">Activity<\/span> {<\/span>\r\n\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 protected void<\/span> onCreate(<span style=\"color: #800080;\">Bundle<\/span> savedInstanceState) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 super<\/span>.onCreate(savedInstanceState);<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 if<\/span> (getResources().getConfiguration().orientation<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 == <span style=\"color: #800080;\">Configuration<\/span>.ORIENTATION_LANDSCAPE) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ If the screen is now in landscape mode, we can show the<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ dialog in-line with the list so we don't need this activity.<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 finish();<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return<\/span>;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 if<\/span> (savedInstanceState == <span style=\"color: #000080;\">null<\/span>) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ During initial setup, plug in the details fragment.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 DetailsFragment<\/span> details = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">DetailsFragment<\/span>();<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 details.setArguments(getIntent().getExtras());<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u6ce8\u610f\u8fd9\u4e2a\u6d3b\u52a8\u5728\u6a2a\u5c4f\u6a21\u5f0f\u65f6\u5c06\u4f1a\u81ea\u52a8\u7ed3\u675f\uff0c\u6539\u4e3a\u7531\u4e3b\u6d3b\u52a8\u540c\u65f6\u663e\u793a<span style=\"color: #008000;\">DetailsFragment<\/span>\u548c<span style=\"color: #008000;\">TitlesFragment<\/span>\u3002\u800c\u5728\u7528\u6237\u4ee5\u7ad6\u5c4f\u6a21\u5f0f\u542f\u52a8<span style=\"color: #008000;\">DetailsActivity<\/span>\u4e4b\u540e\u65cb\u8f6c\u5c4f\u5e55\u81f3\u6a2a\u5c4f\u6a21\u5f0f\u7684\u60c5\u51b5\u4e0b\u4e5f\u4f1a\u5982\u6b64\uff08\u6b64\u65f6\u5c06\u91cd\u542f\u5f53\u524d\u6d3b\u52a8\uff09\u3002<\/p>\n<p>\u5173\u4e8e\u4f7f\u7528\u7247\u6bb5\u7684\u66f4\u591a\u8303\u4f8b\uff08\u4ee5\u53ca\u672c\u8303\u4f8b\u7684\u5b8c\u6574\u6e90\u6587\u4ef6\uff09\uff0c\u8bf7\u53c2\u89c1<a title=\"Fragment\" href=\"http:\/\/developer.android.com\/resources\/samples\/ApiDemos\/src\/com\/example\/android\/apis\/app\/index.html#Fragment\" target=\"_blank\">ApiDemos<\/a>\u91cc\u7684\u8303\u4f8b\u4ee3\u7801\uff08\u53ef\u4ee5\u4ece<a title=\"Getting the Samples\" href=\"http:\/\/developer.android.com\/resources\/samples\/get.html\" target=\"_blank\">\u8303\u4f8bSDK\u7ec4\u4ef6<\/a>\u4e2d\u4e0b\u8f7d\uff09\u3002<\/p>\n<p>\u672c\u9875\u90e8\u5206\u5185\u5bb9\u6839\u636e<a title=\"Android Open Source Projec\" href=\"http:\/\/code.google.com\/intl\/zh-CN\/policies.html\" target=\"_blank\">Android Open Source Project<\/a>\u521b\u4f5c\u5e76\u5171\u4eab\u7684\u5185\u5bb9\u4fee\u6539\uff0c\u5e76\u5728<a title=\"\u77e5\u8bc6\u5171\u4eab \u7f72\u540d2.5\u8bb8\u53ef\u534f\u8bae\" href=\"http:\/\/creativecommons.org\/licenses\/by\/2.5\/\" target=\"_blank\">\u77e5\u8bc6\u5171\u4eab \u7f72\u540d2.5\u8bb8\u53ef\u534f\u8bae<\/a>\u4e2d\u6240\u8ff0\u6761\u6b3e\u7684\u9650\u5236\u4e0b\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u7247\u6bb5\u4ee3\u8868\u4e86\u4e00\u4e2aActivity\u7684\u4e00\u79cd\u884c\u4e3a\u6216\u662f\u5176\u7528\u6237\u754c\u9762\u7684\u4e00\u4e2a\u533a\u57df\u3002\u53ef\u4ee5\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6d3b\u52a8\u4e2d\u7ec4\u5408\u591a\u4e2a\u5206\u7247\u6765\u7ec4\u5efa &hellip; <a href=\"http:\/\/leybreeze.com\/blog\/?p=902\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u7247\u6bb5\uff08Fragment\uff09\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,10],"tags":[5,19,96,17],"_links":{"self":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/902"}],"collection":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=902"}],"version-history":[{"count":31,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions"}],"predecessor-version":[{"id":905,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions\/905"}],"wp:attachment":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}