Sobes.tech
Middle

Kaip įgyvendintum navigaciją naudojant mygtukus, kad ji išsaugotų staką?

sobes.tech AI

Atsakymas iš AI

Naudoti Navigation Architecture Component:

  1. NavGraph sukūrimas: Nustatykite Destinations (fragmentus arba Activity) ir Actions (perėjimus tarp jų) XML ištekliuose:

    <?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>
    
  2. NavHost įrengimas: Pridėkite NavHost (pvz., NavHostFragment) į Activity maketą, kuris susietas su 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" />
    
        <!-- Čia gali būti navigacijos mygtukai -->
    </LinearLayout>
    
  3. Pridėti navigacijos mygtukus: Naudokite BottomNavigationView arba atskirus mygtukus ir susiekite juos su 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="Eiti į B" />
    
        <Button
            android:id="@+id/button_go_to_c"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Eiti į C" />
    </LinearLayout>
    
  4. Kodo dalyje apdorokite mygtukų paspaudimus: Gaukite NavController ir iškvieskite navigate() paspaudus mygtuką. NavController automatiškai valdo back stack ir saugo ankstesnius Destination:

    // Activity arba 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)
        }
    }
    

Jei reikia sudėtingesnės navigacijos su keliomis kortelėmis (BottomNavigationView, DrawerLayout), naudokite setupWithNavController():

// Activity, kur yra 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 pagal numatytuosius nustatymus tvarko back stack. Paspaudus "Atgal" mygtuką, vartotojas grįš į ankstesnį Destination. Norint valdyti back stack elgesį navigate() metu, galima naudoti NavOptions su popUpTo.