NAVIGATION AND FRAGMENTS
Navigation in Android
What is Navigation?
Navigation means moving from one screen to another in an app.
Old Way (Traditional Navigation):
Use Intent to go from one activity to another.
Use FragmentManager to switch between fragments.
New Way (Navigation Component):
This is a Jetpack tool that makes navigation easier and better.
Main Parts:
1. Navigation Graph – a file that shows all screens and how to go from one to another.
2. NavHost – a container that shows the current screen.
3. NavController – the brain that controls where to go.
Steps to Use It:
1. Add these to build.gradle:
groovy
CopyEdit
implementation 'androidx.navigation:navigation-fragment:2.4.1'
implementation 'androidx.navigation:navigation-ui:2.4.1'
2. Make a nav_graph.xml:
xml
CopyEdit
<navigation>
<fragment android:id="@+id/firstFragment">
<action android:id="@+id/action_to_second"
app:destination="@id/secondFragment"/>
</fragment>
<fragment android:id="@+id/secondFragment"/>
</navigation>
3. To move from one fragment to another:
java
CopyEdit
Navigation.findNavController(view).navigate(R.id.action_to_second);
Why use it?
You can pass data easily and safely.
Back button works automatically.
Looks nice with animations.
Supports deep links (e.g., open specific screen from a link).
15. Fragments
What is a Fragment?
A Fragment is like a mini-screen. It is a part of an activity.
Feature Activity = Full screen Fragment = Part of screen
Stands alone? Yes No, needs an Activity
UI Always has UI Can have or not have UI
Reusable? Not much Yes, very reusable
Communicates by Intents Interfaces or ViewModel
Fragment Lifecycle (Important Steps):
onAttach() – fragment is added to activity.
onCreate() – fragment is being created.
onCreateView() – layout is created.
onViewCreated() – after view is ready.
onStart() / onResume() – fragment is visible.
onPause() / onStop() – fragment is not visible.
onDestroyView() – view is removed.
onDestroy() – fragment is removed.
onDetach() – fragment is disconnected from activity.
16. Sending Data Between Fragments
You can send data from one fragment to another in 4 easy ways:
✅1. Using ViewModel (Recommended)
Create a shared ViewModel in activity.
Fragment A sends data to ViewModel.
Fragment B reads it and updates UI.
java
CopyEdit
// In activity
SharedViewModel model = new
ViewModelProvider(this).get(SharedViewModel.class);
// In Fragment A
model.setData("Hello");
// In Fragment B
model.getData().observe(getViewLifecycleOwner(), data -> {
// Use the data
});
✅2. Using Bundle (Arguments)
Send data with a bundle when opening a fragment.
java
CopyEdit
// In Fragment A
FragmentB fragment = new FragmentB();
Bundle args = new Bundle();
args.putString("key", "Hello");
fragment.setArguments(args);
// In Fragment B
String value = getArguments().getString("key");
✅3. Using Interface (Old way)
Create an interface in Fragment A.
Host Activity implements it and sends data to Fragment B.
✅4. Using Navigation Component + Safe Args
Add argument in nav_graph.
xml
CopyEdit
<fragment android:id="@+id/fragmentB">
<argument android:name="argName" app:argType="string"/>
</fragment>
In Fragment A (Kotlin):
kotlin
CopyEdit
val action = FragmentADirections.actionFragmentAToFragmentB("Hello")
findNavController().navigate(action)
In Fragment B (Kotlin):
kotlin
CopyEdit
val args by navArgs<FragmentBArgs>()
val value = args.argName