Kuidas sa rakendaksid navigatsiooni nuppude abil, et see säilitaks virna?
sobes.tech AI
Vastus AI-lt
Navigation Architecture Component kasutamine:
-
NavGraph'i loomine: Määrake Destinations (fragmentid või Activity) ja Actions (üleminekud nende vahel) XML-resurssides:
<?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" android:id="@+id/nav_graph" app:startDestination="@id/fragmentA"> <fragment android:id="@+id/fragmentA" android:name="com.example.FragmentA" android:label="FragmentA" > <action android:id="@+id/action_fragmentA_to_fragmentB" app:destination="@id/fragmentB" /> </fragment> <fragment android:id="@+id/fragmentB" android:name="com.example.FragmentB" android:label="FragmentB" > <action android:id="@+id/action_fragmentB_to_fragmentC" app:destination="@id/fragmentC" /> </fragment> <fragment android:id="@+id/fragmentC" android:name="com.example.FragmentC" android:label="FragmentC" /> </navigation> -
NavHost'i seadistamine: Lisage
NavHost(näiteks,NavHostFragment) tegevuse paigutusse, mis on seotud NavGraph'iga:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.fragment.app.FragmentContainerView android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" /> <!-- Siin võivad olla navigeerimisnupud --> </LinearLayout> -
Lisage navigeerimisnupud: Kasutage
BottomNavigationViewvõi eraldi nuppe ning siduge needNavController-iga:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.fragment.app.FragmentContainerView android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" /> <Button android:id="@+id/button_go_to_b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Mine B" /> <Button android:id="@+id/button_go_to_c" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Mine C" /> </LinearLayout> -
Koodis käsitlege nuppude vajutusi: Saage
NavControllerja kutsugenavigate()nuppu vajutamisel.NavControllerhaldab automaatselt back stack-i, säilitades eelnevad Destination'id:// Activity või Fragment import androidx.navigation.findNavController override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val navController = findNavController(R.id.nav_host_fragment) view.findViewById<Button>(R.id.button_go_to_b).setOnClickListener { navController.navigate(R.id.action_fragmentA_to_fragmentB) } view.findViewById<Button>(R.id.button_go_to_c).setOnClickListener { navController.navigate(R.id.fragmentC) } }
Kui on vaja keerulisemat navigeerimist mitmel vahelehel (BottomNavigationView, DrawerLayout), kasutage setupWithNavController():
// Activity, kus on BottomNavigationView
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.nav_host_fragment)
val bottomNavView = findViewById<BottomNavigationView>(R.id.bottom_nav_view)
bottomNavView.setupWithNavController(navController)
}
Navigation Component haldab vaikimisi back stack-i. Kui vajutate "Tagasi" nuppu, kasutaja naaseb eelmise Destination juurde. navigate()-i käigus back stack-i juhtimiseks saate kasutada NavOptions ja popUpTo.