- dependencies {
- implementation 'androidx.navigation:navigation-fragment:2.5.1'
- implementation 'androidx.navigation:navigation-ui:2.5.1'
- }
- public class HomeFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_home, container, false);
- }
-
- @Override
- public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- NavController controller = Navigation.findNavController(view);
- controller.navigate(R.id.action_homeFragment_to_detailFragment);
- }
- });
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".HomeFragment">
-
- <androidx.constraintlayout.widget.ConstraintLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <androidx.constraintlayout.widget.Guideline
- android:id="@+id/guideline2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- app:layout_constraintGuide_percent="0.5" />
-
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Home"
- android:textSize="24sp"
- app:layout_constraintBottom_toTopOf="@+id/guideline2"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.8" />
-
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="@+id/guideline2"
- app:layout_constraintVertical_bias="0.2" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- </FrameLayout>
- public class DetailFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_detail, container, false);
- }
-
- @Override
- public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- view.findViewById(R.id.button2).setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_detailFragment_to_homeFragment));
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".DetailFragment">
-
- <androidx.constraintlayout.widget.ConstraintLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <androidx.constraintlayout.widget.Guideline
- android:id="@+id/guideline3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- app:layout_constraintGuide_percent="0.5" />
-
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Detail"
- android:textSize="24sp"
- app:layout_constraintBottom_toTopOf="@+id/guideline3"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.8" />
-
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.5"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="@+id/guideline3"
- app:layout_constraintVertical_bias="0.2" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- </FrameLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <navigation xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/my_navigation"
- app:startDestination="@id/homeFragment">
- <fragment
- android:id="@+id/homeFragment"
- android:name="com.example.navigationdemo.HomeFragment"
- android:label="Home"
- tools:layout="@layout/fragment_home">
- <action
- android:id="@+id/action_homeFragment_to_detailFragment"
- app:destination="@id/detailFragment"
- app:enterAnim="@anim/nav_default_enter_anim"
- app:exitAnim="@anim/nav_default_exit_anim" />
- </fragment>
- <fragment
- android:id="@+id/detailFragment"
- android:name="com.example.navigationdemo.DetailFragment"
- android:label="Detail"
- tools:layout="@layout/fragment_detail">
- <action
- android:id="@+id/action_detailFragment_to_homeFragment"
- app:destination="@id/homeFragment"
- app:enterAnim="@anim/nav_default_enter_anim"
- app:exitAnim="@anim/nav_default_exit_anim" />
- </fragment>
- </navigation>
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <androidx.fragment.app.FragmentContainerView
- android:id="@+id/fragmentContainerView"
- android:name="androidx.navigation.fragment.NavHostFragment"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:defaultNavHost="true"
- app:navGraph="@navigation/my_navigation" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
- NavController controller = navHostFragment.getNavController();
- NavigationUI.setupActionBarWithNavController(this, controller);
- }
-
- @Override
- public boolean onSupportNavigateUp() {
- NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
- NavController controller = navHostFragment.getNavController();
- return controller.navigateUp();
- }
- }
