android - fragment 彼此堆叠

标签 android android-fragments kotlin

在Android Studio中,我使用“BottomNavigationView”启动了一个新项目,现在当我在模拟器中运行该应用程序时,默认情况下会存在Home Fragment,但是当我切换到另一个片段时就永远不会替换它。而是,新片段彼此替换,同时仍位于第一个片段的顶部。因此,如果我有3个片段,则片段1停留在 Activity 中,而片段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.导航组件

2.片段管理器

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

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

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

关于android - fragment 彼此堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60908200/

相关文章:

Android Facebook 开发人员无法在 "Save Changes"上为 "Sample App Settings"设置 "Android Key Hash"

Android NDK-arm-linux-androideabi-g++ : not found

android - 如何将 asynctask onpostexecute 方法的结果传递给父 Activity android

android - 如何在抽屉导航中添加 Tabhost

android - 如何在 Kotlin 中保存延迟发帖的状态

kotlin - 从列表中删除不在另一个列表中的元素 - Kotlin

Android adapter.notifyDataSetChanged() 不工作?

android - DialogFragment 上的 commitAllowingStateLoss

android - Fragments ConstraintLayout 是在 activity 的 FrameLayout 中截取的

android - 错误 : failed linking references. -> QueuedJob