Sobes.tech
Middle

Qanday qilib tugmalar yordamida navigatsiyani amalga oshirasiz, shunda u yiginni saqlaydi?

sobes.tech AI

AIdan javob

Navigation Architecture Component ni foydalanardim.

  1. NavGraph yaratish: Destinations (fragmentlar yoki Activitylar) va Actions (ular orasidagi o'tishlar) XML-resursda belgilardim.

    <?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"> // Boshlang'ich nuqta
    
        <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 joylashtirish: NavHost (masalan, NavHostFragment)ni Activity maketiga qo'shardim, unga NavGraph bog'langan.

    <?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" /> // NavGraph bilan bog'lash
    
        <!-- Bu yerda navigatsiya tugmalari bo'lishi mumkin -->
    </LinearLayout>
    
  3. Navigatsiya tugmalarini qo'shish: BottomNavigationView yoki alohida tugmalarni ishlatardim. BottomNavigationViewni NavControllerga bog'lagan bo'lardim.

    <?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 ga o'tish" />
    
        <Button
            android:id="@+id/button_go_to_c"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C ga o'tish" />
    </LinearLayout>
    
  4. Kodda tugmalar bosilishi bilan ishlash: NavController nusxasini olardim va navigate() chaqirardim. NavController avtomatik ravishda back stackni boshqaradi, oldingi Destinationlarni saqlab qoladi.

    // Activity yoki Fragmentda
    import androidx.navigation.findNavController
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    
        val navController = findNavController(R.id.nav_host_fragment) // NavController olish
    
        view.findViewById<Button>(R.id.button_go_to_b).setOnClickListener {
            navController.navigate(R.id.action_fragmentA_to_fragmentB) // Action orqali o'tish
        }
    
        view.findViewById<Button>(R.id.button_go_to_c).setOnClickListener {
            // To'g'ridan-to'g'ri Destinationga o'tish, agar mavjud bo'lsa, action_fragmentA_to_fragmentC ishlatish mumkin
             navController.navigate(R.id.fragmentC)
        }
    }
    

Ko'proq murakkab navigatsiya uchun, bir nechta tablar (BottomNavigationView, DrawerLayout) bilan, setupWithNavController() ishlatardim.

// BottomNavigationView bilan Activityda
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) // BottomNavigationViewni NavControllerga bog'lash
}

Navigation Component default ravishda back stackni boshqaradi. "Orqaga" tugmasi bosilganda, foydalanuvchi oldingi destinationga qaytadi. navigate() bilan back stackni boshqarish uchun, popUpTo bilan NavOptions ishlatish mumkin.