最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - Bottom Navigation Bar not working properly when defining startDestination - Stack Overflow

programmeradmin1浏览0评论

I'm having a strange issue. First the code.

MainAcitvity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val einstellungenDatei = File(this.filesDir, "einstellungen")

        if (!einstellungenDatei.exists()) {
            einstellungenDatei.createNewFile()

            einstellungenDatei.writeText("startbildschirm=0\nkraftstoffart=0")
        }

        var startbildschirm = ""

        einstellungenDatei.forEachLine { line ->
            if (line.startsWith("startbildschirm=")) {
                startbildschirm = line.replace("startbildschirm=","")
            }
        }

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navView: BottomNavigationView = binding.navView
        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_direktsuche, R.id.navigation_kartensuche, R.id.navigation_favoriten
            )
        )

        if (startbildschirm == "0"){
            navController.navigate(R.id.navigation_direktsuche)
        }
        else if (startbildschirm == "1"){
            navController.navigate(R.id.navigation_kartensuche)
        }
        else {
            navController.navigate(R.id.navigation_favoriten)
        }

        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=";
    xmlns:app=";
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu"
        android:fitsSystemWindows="true"/>

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation"/>

</androidx.constraintlayout.widget.ConstraintLayout>

mobile_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android=";
    xmlns:app=";
    xmlns:tools=";
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_favoriten">

    <fragment
        android:id="@+id/navigation_direktsuche"
        android:name="com.example.tankn.ui.direktsuche.DirektsucheFragment"
        android:label="@string/Direktsuche"
        tools:layout="@layout/fragment_direktsuche"/>

    <fragment
        android:id="@+id/navigation_kartensuche"
        android:name="com.example.tankn.ui.kartensuche.KartensucheFragment"
        android:label="@string/Kartensuche"
        tools:layout="@layout/fragment_kartensuche" />

    <fragment
        android:id="@+id/navigation_favoriten"
        android:name="com.example.tankn.ui.favoriten.FavoritenFragment"
        android:label="@string/Favoriten"
        tools:layout="@layout/fragment_favoriten"/>
</navigation>

Whatever is in app:startDestination is not selectable in the bottom navigation bar. In this example, if I start the App, KartensucheFragment is opened. If I click on Direktsuche the screen changes to this. But if Favoriten is clicked, nothing happens. Just the sound of a click is played.

But if I comment this out, everything works as expected

    if (startbildschirm == "0"){
        navController.navigate(R.id.navigation_direktsuche)
    }
    else if (startbildschirm == "1"){
        navController.navigate(R.id.navigation_kartensuche)
    }
    else {
        navController.navigate(R.id.navigation_favoriten)
    }

what can I do to make this work?

发布评论

评论列表(0)

  1. 暂无评论