{"id":985,"date":"2015-08-18T17:40:56","date_gmt":"2015-08-18T09:40:56","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=985"},"modified":"2026-08-18T12:58:44","modified_gmt":"2026-08-18T04:58:44","slug":"fragment%e6%a0%88","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=985","title":{"rendered":"Fragment\u6808"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">Activity \u6709\u4efb\u52a1\u6808\uff0cFragment \u4e5f\u6709\u56de\u9000\u6808\u3002\u628a Fragment <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">addToBackStack<\/code> \u4e4b\u540e\u70b9\u8fd4\u56de\uff0c\u5c31\u80fd\u56de\u5230\u4e0a\u4e00\u4e2a Fragment\u3002\u4e0b\u9762\u5bf9\u7167 APIDemo \u7684 FragmentStack\u3002<\/p>\n<\/blockquote>\n<p><!--more--><\/p>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e00\u3001\u6548\u679c\u4e0e\u5e03\u5c40<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">Push \u538b\u6808\u3001Pop \u5f39\u6808\uff0c\u4e2d\u95f4 FrameLayout \u627f\u8f7d\u5f53\u524d Fragment\uff1a<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/08\/fragment_stack.gif\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-986\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/08\/fragment_stack.gif\" alt=\"fragment_stack\" width=\"322\" height=\"533\" \/><\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u5e03\u5c40 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">fragment_stack.xml<\/code>\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:gravity=\"center_horizontal\"\n    android:orientation=\"vertical\"\n    android:padding=\"4dip\" &gt;\n    &lt;LinearLayout\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_weight=\"0\"\n        android:gravity=\"center_horizontal\"\n        android:measureWithLargestChild=\"true\"\n        android:orientation=\"horizontal\"\n        android:padding=\"4dip\" &gt;\n        &lt;Button\n            android:id=\"@+id\/new_fragment\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:text=\"Push\" &gt;\n            &lt;requestFocus \/&gt;\n        &lt;\/Button&gt;\n        &lt;Button\n            android:id=\"@+id\/delete_fragment\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:layout_weight=\"0\"\n            android:text=\"Pop\" \/&gt;\n    &lt;\/LinearLayout&gt;\n    &lt;FrameLayout\n        android:id=\"@+id\/simple_fragment\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"0px\"\n        android:layout_weight=\"1\" \/&gt;\n&lt;\/LinearLayout&gt;<\/code><\/pre>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e8c\u3001\u538b\u6808\u4e0e\u5f39\u6808<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">Push\uff1a<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">mStackLevel++<\/code> \u540e <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">replace<\/code> + <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">addToBackStack(null)<\/code>\u3002Pop\uff1a<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">mStackLevel--<\/code> \u518d <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">popBackStack()<\/code>\u3002CountingFragment \u7528 arguments \u4f20\u5165\u5e8f\u53f7\uff0c\u80cc\u666f\u8272\u6309 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">mNum % 3<\/code> \u8f6e\u6362\u84dd\u7eff\u6a59\uff08\u6570\u7ec4\u91cc\u8fd8\u6709\u7ea2\u8272\u672a\u7528\u5230\uff09\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class FragmentStack extends Activity {\n    int mStackLevel = 0;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.fragment_stack);\n        Button button = (Button) findViewById(R.id.new_fragment);\n        button.setOnClickListener(new OnClickListener() {\n            public void onClick(View v) {\n                mStackLevel++;\n                addFragmentToStack();\n            }\n        });\n        button = (Button) findViewById(R.id.delete_fragment);\n        button.setOnClickListener(new OnClickListener() {\n            public void onClick(View v) {\n                mStackLevel--;\n                getFragmentManager().popBackStack();\n            }\n        });\n    }\n\n    void addFragmentToStack() {\n        Fragment newFragment = CountingFragment.newInstance(mStackLevel);\n        FragmentTransaction ft = getFragmentManager().beginTransaction();\n        ft.replace(R.id.simple_fragment, newFragment);\n        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);\n        ft.addToBackStack(null);\n        ft.commit();\n    }\n\n    public static class CountingFragment extends Fragment {\n        int mNum;\n        private static int[] sColorValue = new int[] {\n                android.R.color.holo_blue_light,\n                android.R.color.holo_green_light,\n                android.R.color.holo_orange_light,\n                android.R.color.holo_red_light };\n\n        static CountingFragment newInstance(int num) {\n            CountingFragment f = new CountingFragment();\n            Bundle args = new Bundle();\n            args.putInt(\"num\", num);\n            f.setArguments(args);\n            return f;\n        }\n\n        @Override\n        public void onCreate(Bundle savedInstanceState) {\n            super.onCreate(savedInstanceState);\n            mNum = getArguments() != null ? getArguments().getInt(\"num\") : 1;\n        }\n\n        @Override\n        public View onCreateView(LayoutInflater inflater, ViewGroup container,\n                Bundle savedInstanceState) {\n            View v = inflater.inflate(R.layout.fragment_stack_layout, container, false);\n            View tv = v.findViewById(R.id.text);\n            ((TextView) tv).setText(\"Fragment #\" + mNum);\n            tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));\n            Log.v(\"TAG\", \"mNum % 3 \" + mNum % 3);\n            tv.setBackgroundColor(getActivity().getResources().getColor(sColorValue[mNum % 3]));\n            return v;\n        }\n    }\n}<\/code><\/pre>\n<table style=\"border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; text-align: left; overflow: auto; display: table; overflow-wrap: break-word; word-break: break-all;\" width=\"817\">\n<thead>\n<tr>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u64cd\u4f5c<\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u4ee3\u7801<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u538b\u6808<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">replace \u540e ft.addToBackStack(null)<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u5f39\u6808<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">getFragmentManager().popBackStack()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">FragmentTransaction ft = getFragmentManager().beginTransaction();\nft.addToBackStack(null);\n\ngetFragmentManager().popBackStack();<\/code><\/pre>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><strong>\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1areplace \u4e4b\u540e\u5fc5\u987b addToBackStack\uff0c\u8fd4\u56de\u952e \/ popBackStack \u624d\u80fd\u56de\u5230\u4e0a\u4e00\u4e2a Fragment\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Activity \u6709\u4efb\u52a1\u6808\uff0cFragment \u4e5f\u6709\u56de\u9000\u6808\u3002\u628a Fragment addToBackStack  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[67],"tags":[68],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/985"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=985"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/985\/revisions"}],"predecessor-version":[{"id":989,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/985\/revisions\/989"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}