碎片彼此堆叠

时间:2020-03-28 23:15:39

标签: android android-fragments kotlin

在Android Studio中,我使用“ BottomNavigationView”启动了一个新项目,现在当我在模拟器中运行该应用程序时,默认情况下存在Home Fragment,但是当我切换到另一个Fragment时就永远不会替换它。而是,新片段彼此替换,同时仍位于第一个片段的顶部。因此,如果我有3个片段,则片段1停留在活动中,而片段1(重复),片段2和片段3都在片段1(原始)的顶部相互替换。我不知道如何摆脱原始片段,甚至从何而来。我是Android开发和学习Kotlin的新手,所以我很难在Kotlin中找到建议。这是我的MainActivity中的代码:

package com.example.android.pointmax


import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.android.pointmax.ui.home.HomeFragment
import com.example.android.pointmax.ui.recommended.RecommendedFragment
import com.example.android.pointmax.ui.wallet.WalletFragment
import timber.log.Timber


class MainActivity : AppCompatActivity() {

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home-> {
                val fragment = HomeFragment.newInstance()
                replaceFragment(fragment)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_wallet -> {
                replaceFragment(WalletFragment())
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_recommended -> {
                replaceFragment(RecommendedFragment())
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Plant tree to enable Debugging with Timber
        Timber.plant(Timber.DebugTree())

        // Find the bottomNavigation bar
        val navView: BottomNavigationView = findViewById(R.id.nav_view)

        // Find the fragment that will host the different fragments
        val navController = findNavController(R.id.nav_host_fragment)

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(setOf(
                R.id.navigation_home, R.id.navigation_wallet, R.id.navigation_recommended))
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

        // Set click listeners to respond to bottom navigation selections
        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

    }

    private fun replaceFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.nav_host_fragment,fragment)
            .commit()
    }
}

MainActivity.xml是默认情况下的创建方式,带有ConstraintLayout,在其下面嵌套有bottomNavigationView和nav_host_fragment。我非常感谢您的帮助。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

出现这种奇怪的导航行为的原因是因为您正在同时尝试两种导航方法

1。导航组件

2。片段管理器

使用Android Jetpack的导航组件时,您应该创建导航图并将其用于片段之间的导航。

此链接应帮助您了解有关导航图的更多信息。

https://developer.android.com/guide/navigation/navigation-design-graph