
If you create fragment without create its Activity, simple do like the following code : The part of the code above available on MainActivity, inside button onClick But when this class is loaded, by using attribute class inside tagline fragment, then automatically its fragment will loadedĬalling Fragment NameFragment from MainActivity, you can do it look like the following code : Then create a java class for Activity NameFragment, name it as ActForFragmentNameActivity.java, the code is looks like the follows :Īs we can see there is not special initialize code inside class MainActivity, it just initialize of its layout. NameFragment : name of class Fragment that we created From where the class ? that is the name of class Fragment that we create before, that is NameFragment.Ĭom. contains name of package project and name of class.Ĭom.agmenttoactivity : name of package project that we created


Take a look at above code, inside tag there is an attribute class, that contains class Fragment. So how can we call Fragment NameFragment from MainActivity?Ĭreate a new layout name it as activity_for_fragment_name.xml Next create layout for Fragment NameFragment name it as fragment_name.xml, here is the code : Well, above is a class that we used as the first page when application is openedĬreate a class Fragment and name it as NameFragment.java, Here is the code : Code for class MainActivity.java looks like the follows : Layout activity_main.xml will used on class MainActivity. So when you try to access a Fragment, just call the Activity where you set the fragment using Intent.Ĭreate a XML file name it as activity_main.xml We call the fragment in Activity's layout. On this part, i will show you two ways how to move from Activity to Fragment: After learn about Activity, that they will learn about how to move from Activity to Fragment, and the Fragment to Activity After that the next step is learn how to move from one Activity to another Activity using Intent.
