\n <\/td>\n | private<\/span> static<\/span> final<\/span> String<\/span> TAG_MY_FRAGMENT<\/span> = "myFragment"<\/span>;<\/td>\n <\/tr>\n \n <\/td>\n | \n<\/td>\n <\/tr>\n | \n <\/td>\n | private<\/span> MyFragment<\/span> mFragment<\/span>;<\/td>\n <\/tr>\n \n <\/td>\n | \n<\/td>\n <\/tr>\n | \n <\/td>\n | @<\/span>Override<\/span><\/td>\n <\/tr>\n \n <\/td>\n | protected<\/span> void<\/span> onCreate<\/span>(Bundle<\/span> savedInstanceState<\/span>) {<\/td>\n <\/tr>\n \n <\/td>\n | super<\/span>.onCreate<\/span>(savedInstanceState<\/span>);<\/td>\n <\/tr>\n \n <\/td>\n | <\/td>\n <\/tr>\n | \n <\/td>\n | setContentView<\/span>(R<\/span>.layout<\/span>.activity_adding_fragments<\/span>);<\/td>\n <\/tr>\n \n <\/td>\n | <\/td>\n <\/tr>\n | \n <\/td>\n | if<\/span> (savedInstanceState<\/span> == null<\/span>) {<\/td>\n <\/tr>\n \n <\/td>\n | // The Activity is NOT being re-created so we can instantiate a new Fragment<\/span><\/td>\n <\/tr>\n \n <\/td>\n | // and add it to the Activity<\/span><\/td>\n <\/tr>\n \n <\/td>\n | mFragment<\/span> = new<\/span> MyFragment<\/span>();<\/td>\n <\/tr>\n \n <\/td>\n | <\/td>\n <\/tr>\n | \n <\/td>\n | getSupportFragmentManager<\/span>()<\/td>\n <\/tr>\n \n <\/td>\n | .beginTransaction<\/span>()<\/td>\n <\/tr>\n \n <\/td>\n | // It's almost always a good idea to use .replace instead of .add so that<\/span><\/td>\n <\/tr>\n \n <\/td>\n | // you never accidentally layer multiple Fragments on top of each other<\/span><\/td>\n <\/tr>\n \n <\/td>\n | // unless of course that's your intention<\/span><\/td>\n <\/tr>\n \n <\/td>\n | .replace<\/span>(R<\/span>.id<\/span>.content_frame<\/span>, mFragment<\/span>, TAG_MY_FRAGMENT<\/span>)<\/td>\n <\/tr>\n \n <\/td>\n | .commit<\/span>();<\/td>\n <\/tr>\n \n <\/td>\n | } else<\/span> {<\/td>\n <\/tr>\n \n <\/td>\n | // The Activity IS being re-created so we don't need to instantiate the Fragment or add it,<\/span><\/td>\n <\/tr>\n \n <\/td>\n | // but if we need a reference to it, we can use the tag we passed to .replace<\/span><\/td>\n <\/tr>\n \n <\/td>\n | mFragment<\/span> = (MyFragment<\/span>) getSupportFragmentManager<\/span>().findFragmentByTag<\/span>(TAG_MY_FRAGMENT<\/span>);<\/td>\n <\/tr>\n \n <\/td>\n | }<\/td>\n <\/tr>\n | \n <\/td>\n | }<\/td>\n <\/tr>\n | \n <\/td>\n | ...<\/td>\n <\/tr>\n | \n <\/td>\n | public<\/span> void<\/span> myMethod<\/span>() {<\/td>\n <\/tr>\n \n <\/td>\n | // Later on in the code we can do something with the Fragment<\/span><\/td>\n <\/tr>\n \n <\/td>\n | mFragment<\/span>.doSomething<\/span>();<\/td>\n <\/tr>\n \n <\/td>\n | }<\/td>\n <\/tr>\n <\/table>\n<\/div>\n\n\n <\/div>\n\n <\/div>\n<\/div>\n\n <\/div>\n | | | | | | | | | | | | | | | | | | | | | | | | |