Ինչպե՞ս կկատարեիր նավիգացիան կոճակների օգտագործմամբ, որպեսզի պահպանի stack-ը։
sobes.tech AI
Պատասխան AI-ից
Օգտագործել Navigation Architecture Component:
-
NavGraph-ի ստեղծում: Դիտարկեք Destinations (ֆրագմենտներ կամ Activity) և Actions (անցումներ դրանց միջև) XML ռեսուրսում:
<?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-ի տեղադրում: Ավելացրեք
NavHost(օրինակ,NavHostFragment) Activity-ի ձևանմուշում, որի վրա կապված է NavGraph:<?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" /> <!-- Այստեղ կարող են լինել նավիգացիայի կոճակներ --> </LinearLayout> -
Նավիգացիայի կոճակների ավելացում: Օգտագործեք
BottomNavigationViewկամ առանձին կոճակներ։BottomNavigationView-ին կապեքNavController-ի հետ:<?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="Գնալ B" /> <Button android:id="@+id/button_go_to_c" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Գնալ C" /> </LinearLayout> -
Կոդում կոճակների սեղմումների մշակումը: Ստացեք
NavController-ի օրինակ և կանչեքnavigate()-ը սեղմման ժամանակ։NavController-ը ավտոմատ կերպով կառավարում է back stack-ը, պահելով նախորդ Destination-ները:// Activity կամ 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) } }
Եթե անհրաժեշտ է ավելի բարդ նավիգացիա՝ մի քանի տաբներով (BottomNavigationView, DrawerLayout), օգտագործեք setupWithNavController():
// Activity-ում, որտեղ կա 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-ը ըստ նախնական կարգավորումների կառավարում է back stack-ը։ Երբ սեղմում եք "Ներքևի" կոճակը, օգտվողը վերադառնում է նախորդ Destination։ Կարող եք կառավարել navigate()-ի ժամանակ back stack-ի վարքը՝ օգտագործելով NavOptions-ը և popUpTo։