{"action":"opened","issue":{"url":"https://api.github.com/repos/fuhongliang/Android_Beginners/issues/2","labels_url":"https://api.github.com/repos/fuhongliang/Android_Beginners/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/fuhongliang/Android_Beginners/issues/2/comments","events_url":"https://api.github.com/repos/fuhongliang/Android_Beginners/issues/2/events","html_url":"https://github.com/fuhongliang/Android_Beginners/issues/2","id":76990870,"number":2,"title":"#基础# Fragment基础使用","user":{"login":"fuhongliang","id":7702246,"avatar_url":"https://avatars.githubusercontent.com/u/7702246?v=3","gravatar_id":"","url":"https://api.github.com/users/fuhongliang","html_url":"https://github.com/fuhongliang","followers_url":"https://api.github.com/users/fuhongliang/followers","following_url":"https://api.github.com/users/fuhongliang/following{/other_user}","gists_url":"https://api.github.com/users/fuhongliang/gists{/gist_id}","starred_url":"https://api.github.com/users/fuhongliang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fuhongliang/subscriptions","organizations_url":"https://api.github.com/users/fuhongliang/orgs","repos_url":"https://api.github.com/users/fuhongliang/repos","events_url":"https://api.github.com/users/fuhongliang/events{/privacy}","received_events_url":"https://api.github.com/users/fuhongliang/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-05-16T09:20:46Z","updated_at":"2015-05-16T09:20:46Z","closed_at":null,"body":"### 添加Fragment的方式有两种，一种是定义在XML文件中，一种是动态添加（需要xml中定义一个空的FrameLayout）\r\n\r\n###### 动态添加\r\n创建想要添加的Fragment \r\nHeadlinesFragment firstFragment = new HeadlinesFragment();\r\n如果希望传递参数可以这样设置\r\nfirstFragment.setArguments(getIntent().getExtras());\r\n获取SupportFragmentManager、开始事务beginTransaction、进行的是添加操作add、提交commit \r\ngetSupportFragmentManager().beginTransaction().add(R.id.fragment_container,firstFragment).commit();\r\n\r\n###### 获取定义在Xml中的Fragment\r\nArticleFragment articleFrag = (ArticleFragment)\r\ngetSupportFragmentManager().findFragmentById(R.id.article_fragment);\r\n\r\n定义在xml文件中的Fragment不可动态移除掉\r\n\r\n### 进行替换Fragment操作\r\n\r\n FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\r\n transaction.replace(R.id.fragment_container, newFragment);\r\n 如果想回到替换前的Fragment，添加返回导航\r\n transaction.addToBackStack(null);\r\n transaction.commit();\r\n\r\n#### Fragment to Activity communication\r\n\r\n在Fragment中定义一个接口，需要在Activity中实现该接口，在onAttach(Activity activity)方法中判断Activity是否已实现了接口。\r\n``` java\r\nDefine an Interface\r\n// Container Activity must implement this interface\r\n    public interface OnHeadlineSelectedListener {\r\n        public void onArticleSelected(int position);\r\n    }\r\n\r\n @Override\r\n    public void onAttach(Activity activity) {\r\n        super.onAttach(activity);\r\n       \r\n        // This makes sure that the container activity has implemented\r\n        // the callback interface. If not, it throws an exception\r\n        try {\r\n            mCallback = (OnHeadlineSelectedListener) activity;\r\n        } catch (ClassCastException e) {\r\n            throw new ClassCastException(activity.toString()\r\n                    + \" must implement OnHeadlineSelectedListener\");\r\n        }\r\n    }\r\n``` \r\n\r\n\r\n通过在Fragment中调用接口中的方法，即可与Activity communication。\r\n\r\n### All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.\r\n\r\n### Deliver a Message to a Fragment\r\n\r\n在Activity中获取Fragment的实例，判断是否非空，调用Fragment中的公共函数，通过参数传递Message。"}}