For those who learning android programming for the first time, they will learn about android components like Java Class, XML, Activity, Fragment, Adapter, etc. Show
After that the next step is learn how to move from one Activity to another Activity using Intent. After learn about Activity, that they will learn about how to move from Activity to Fragment, and the Fragment to Activity Move From Activity To FragmentOn this part, i will show you two ways how to move from Activity to Fragment: We call the fragment in Activity's layout. So when you try to access a Fragment, just call the Activity where you set the fragment using Intent. Here is the example :Create a XML file name it as activity_main.xml Layout activity_main.xml will used on class MainActivity. Code for class MainActivity.java looks like the follows : Well, above is a class that we used as the first page when application is openedCreate a class Fragment and name it as NameFragment.java, Here is the code : Next create layout for Fragment NameFragment name it as fragment_name.xml, here is the code : OK, class Fragment already done. So how can we call Fragment NameFragment from MainActivity?Create a new layout name it as activity_for_fragment_name.xml Take a look at above code, inside tag there is an attribute class, that contains class Fragment. From where the class com.putuguna.fragmenttoactivity.NameFragment? that is the name of class Fragment that we create before, that is NameFragment. com.putuguna.fragmenttoactivity.NameFragment contains name of package project and name of class. Then create a java class for Activity NameFragment, name it as ActForFragmentNameActivity.java, the code is looks like the follows : As we can see there is not special initialize code inside class MainActivity, it just initialize of its layout. But when this class is loaded, by using attribute class inside tagline fragment, then automatically its fragment will loaded Calling Fragment NameFragment from MainActivity, you can do it look like the following code : The part of the code above available on MainActivity, inside button onClick If you create fragment without create its Activity, simple do like the following code : If you try access an Activity from a Fragment, simply like the following code : Yang perlu diperhatikan adalah :.... new Intent(getActivity(), .....), getActivity() as Activity of Fragment, because on Intent's constructor required Activity and destination class , so we use getActivity() to take fragment's activity. And also before you do startActivity(), first write getActivity() then startActivity().That's all. Sorry for worst english. Thank you.
Switching between fragments within an activity in Android has been a painful experience in the past. Using the fragment manager and fragment transactions to switch between fragments was never a great developer experience. As a part of the Android Jetpack suite of libraries and tools, the Navigation Architecture Component has been made available to Android developers to help simplify navigation within your Android app. To move between fragments within an activity using the Navigation Architecture Component in JetPack, you need to follow these steps.
In the blog post below I will take you through a step by step guide on how to use the navigation component in Java to navigate between two different fragments within an app with a single activity. A video version of this tutorial is available on YouTube which is also embedded below. https://www.youtube.com/watch?v=auZCPpDCT1w Navigation Architecture Component OverviewWhat are the benefits of using the Navigation Architecture Component?Some of the benefits of using the Navigation Architecture Component quoted from the official documentation include the following.
What is the Navigation Architecture Component?The Navigation Architecture Component comprises of a number of different sub components which are used to handle navigation within an Android app, these include the following.
The Navigation Graph is a new resource type used to centralise information relating to navigation within your Android app including Destinations and Actions. Destinations are views that you can navigate to within your app such as Fragments or Activities. Actions represent the transitions a user can make between Destinations inside your app. The Navigation Graph is an XML file that can be modified manually but it can also be changed using the Navigation Editor available in Android Studio 3.3 and above. The Navigation Editor shows a visual representation of your Navigation Graph in a directed graph. The nodes displayed in the Navigation Editor are the Destinations and the nodes are connected with the Actions. The Navigation Editor allows you to view and modify the Desintations and Actions to you will have in your app. The changes when made in the editor will be reflected in the Navigation Graph resource XML file allowing you to keep to track of changes in version control. The NavHostFragment is placed inside the layout of your Activity and acts as a window allowing you to show the correct Fragment Destination within your Navigation Graph and swap it out with other Fragment Destinations as the user navigates through your app. The NavController is used to navigate to a particular Destination within your Navigation Graph by referencing either a Destination or Action. The SafeArgs plugin can be used to support type safe navigation and argument passing when using the NavController. Using Navigation Architecture Component in JavaStep 1: Adding the Navigation Component DependenciesIn order to use the navigation component in your Android project you will need to include the latest navigation-ui and navigation-fragment dependencies in your app’s build.grade file. See the following excerpt from a build.gradle file in the app directory which includes the latest navigation architecture component dependencies. It is also recommended that you update Android Studio to the latest version when using the navigation component as it requires a minimum Android Studio version of 3.3. Step 2: Creating the Navigation Graph ResourceNote: This step will utilise the Navigation Editor in Android Studio, please make sure Android Studio is at least version 3.3 before you proceed. The Navigation Graph is a new resource that will contain all of the information relating to navigation including all of the paths between destinations within your Android app. To create a Navigation Graph resource in Android Studio, follow the steps below. How to create a Navigation Graph in Android Studio 3.3+
Once the Navigation Graph is created you will be able to view the Navigation Graph in the “Design” view and in the “Text” view. The “Design” view shows the Navigation Graph in the Navigation Editor. Empty Navigation Graph in the Navigation EditorThe “Text” view shows the Navigation Graph as a text based XML file. Empty Navigation Graph in the text editorAt the moment the Navigation Graph is empty, later in this tutorial I will show you how to add Fragments as Destinations and create Actions that allow you to navigate between Fragment Destinations. Step 3: Adding the NavHostFragment to the Activity LayoutIn order to navigate between fragments using the navigation architecture component we will need to be able to show the relevant fragment to the user based on where the user has navigated to within the app. We will be placing a NavHostFragment inside the layout of the MainActivity which will act like a window for showing the right fragment back to the user as they navigate through the app. The NavHostFragment is added via a fragment tag, the following attributes relating to navigation are set on the fragment tag.
See an excerpt below which shows a NavHostFragment being added to the MainActivity layout. If we run the app now it will crash as we have not yet added any fragments as destinations to the navigation graph. That means the app will not know what to show inside the NavHostFragment window. We will cover the creation of fragments and adding them to the navigation graph in the next step. Step 4: Adding Fragments to the Navigation Graph as DestinationsFor purposes of demonstrating the basic usage of the navigation architecture component I will be creating two simple fragments to be shown in this app, Fragment1 and Fragment2. They will both contain a simple TextView with the text “Fragment 1” and “Fragment 2” correspondingly. Fragment1 will also contain a button which will be used later in the tutorial to navigate to Fragment2. See the code samples below for the Fragment1 and Fragment2 classes and layout files. Fragment 1 Code Samples Fragment 2 Code Samples Now that these fragments have been created we will now proceed to add them as destinations to the navigation graph using the Navigation Editor inside Android Studio. How to Add Fragment Destinations to a Navigation Graph in Android Studio 3.3+
As you can see, the two Fragment Destinations inside the Navigation Graph are not currently linked, meaning there is no navigation pathway between these destinations. In the next step will cover creating a navigation pathway within the Navigation Graph using Actions. Step 5: Creating Actions enabling navigation between Destinations in the Navigation GraphActions are used in the navigation architecture component to create a pathway between two destinations. We will be creating one Action to be used to navigate from Fragment 1 to Fragment 2. This transition will occur on the selection of a button press inside Fragment 1. We will create the Action inside the Navigation Editor inside the visual editor in the Design tab. To do this first select fragment 1 in the Navigation Editor which should highlight the fragment in a blue border. Then select the blue circle to the right of the fragment and drag the circle over fragment 2, then release it. This should create an Action and show an arrow pointing towards fragment 2 from fragment 1. To make use of this Action to navigate between fragment 1 to fragment 2, in the next step we will wire up the button in fragment 1 to navigate the user to fragment 2 within the NavHostFragment when selected. Step 6: Using the NavController to Programmatically Navigate Between FragmentsThe NavController is used to navigate to a specific Destination within the navigation graph by referencing a Destination or Action. In the Fragment 1 class we will make some changes to add an onClickListener to the button to used to navigate to Fragment 2. Inside the onClickListener we will retrieve locate the NavController using the Navigation.findNavController(view) method, then call the navigate method and pass the Action reference to navigate to fragment 2. See the following code sample in the Fragment1 class showing the navigation to Fragment2. If you make these changes and run the app you will see the app is transitioned to Fragment 2 from Fragment 1. If you use the back button you will see it has been preserved and will take you back to Fragment 1. All code samples from this tutorial are available in the GitHub repository linked below. https://github.com/learntodroid/AndroidNavigationComponentTutorial |