Sobes.tech
Middle

Wie würdest du die Navigation mit Buttons implementieren, damit sie den Stack beibehält?

sobes.tech KI

Antwort von AI

Ich würde die Navigation Architecture Component verwenden.

  1. Erstellen des NavGraph: Ich würde Ziele (Fragmente oder Activities) und Aktionen (Übergänge zwischen ihnen) in einer XML-Ressource definieren.

    <?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"> // Startpunkt
    
        <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. Platzierung des NavHost: Ich würde einen NavHost (z.B. NavHostFragment) im Layout der Activity hinzufügen, der mit dem NavGraph verbunden ist.

    <?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" /> // Verknüpfung mit NavGraph
    
        <!-- Hier können Navigationsbuttons sein -->
    </LinearLayout>
    
  3. Hinzufügen von Navigationsbuttons: Ich würde BottomNavigationView oder separate Buttons verwenden. Für BottomNavigationView würde ich es mit NavController verbinden.

    <?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="Gehe zu B" />
    
        <Button
            android:id="@+id/button_go_to_c"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gehe zu C" />
    </LinearLayout>
    
  4. Verarbeitung von Button-Klicks im Code: Ich würde den NavController erhalten und navigate() beim Klick auf den Button aufrufen. Der NavController verwaltet automatisch den Back-Stack, wobei vorherige Ziele gespeichert werden.

    // In der Activity oder im Fragment
    import androidx.navigation.findNavController
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    
        val navController = findNavController(R.id.nav_host_fragment) // NavController erhalten
    
        view.findViewById<Button>(R.id.button_go_to_b).setOnClickListener {
            navController.navigate(R.id.action_fragmentA_to_fragmentB) // Navigation via Action
        }
    
        view.findViewById<Button>(R.id.button_go_to_c).setOnClickListener {
            // Beispiel für direkte Navigation zu einem Ziel, wenn vorhanden, kann auch action_fragmentA_to_fragmentC genutzt werden
             navController.navigate(R.id.fragmentC)
        }
    }
    

Für komplexere Navigation mit mehreren Tabs (BottomNavigationView, DrawerLayout) würde ich setupWithNavController() verwenden.

// In der Activity mit 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) // Verbindung des BottomNavigationView mit NavController
}

Der Navigation Component verwaltet standardmäßig den Back-Stack. Das Drücken des "Zurück"-Buttons bringt den Nutzer zum vorherigen Ziel im Stack. Um das Verhalten des Back-Stacks bei navigate() zu steuern, kann man NavOptions mit popUpTo verwenden.