Android Fragment.hide()不隐藏我的片段

Android Fragment.hide()不隐藏我的片段,android,xml,android-fragments,kotlin,Android,Xml,Android Fragments,Kotlin,我已经实现了hide()Fragment的最基本形式,但它根本不起作用 我的初始onCreate()将所有3个片段添加到活动中(它们都同时显示)。然后,当单击导航项时,我对两个片段调用hide(),但是它们不会从屏幕上消失,没有任何变化 我的活动: class TruthOrDare : FragmentActivity(), BottomNavigationView.OnNavigationItemSelectedListener { override fun onCreate(s

我已经实现了
hide()
Fragment的最基本形式,但它根本不起作用

我的初始
onCreate()
将所有3个片段添加到活动中(它们都同时显示)。然后,当单击导航项时,我对两个片段调用
hide()
,但是它们不会从屏幕上消失,没有任何变化

我的活动:

class TruthOrDare : FragmentActivity(), BottomNavigationView.OnNavigationItemSelectedListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_truth_or_dare)
        bottomNav_truthordare.setOnNavigationItemSelectedListener(this)
        bottomNav_truthordare.selectedItemId = R.id.action_game
        if (savedInstanceState != null) {
            return
        }
        firstFragment.arguments = intent.extras
        val transaction = supportFragmentManager.beginTransaction()
        transaction.add(R.id.fragment_layout, GameFragment())
        transaction.add(R.id.fragment_layout, LeaderboardsFragment())
        transaction.add(R.id.fragment_layout, OptionsFragment())
        transaction.commit()

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.hide(GameFragment())
        transaction.hide(OptionsFragment())
        transaction.commit()

        return true
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_truthordare"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    tools:context=".TruthOrDare">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_truthordare"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimaryDark"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@color/colorPrimaryDark"
        app:title="Level 1" />

    <FrameLayout
        android:id="@+id/fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomNav_truthordare" android:layout_below="@id/toolbar_truthordare">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNav_truthordare"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:itemIconTint="@color/bottom_nav_items"
        app:itemTextColor="@color/bottom_nav_items"
        android:layout_alignParentBottom="true"
        app:menu="@menu/menu" />

</RelativeLayout>
我的XML:

class TruthOrDare : FragmentActivity(), BottomNavigationView.OnNavigationItemSelectedListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_truth_or_dare)
        bottomNav_truthordare.setOnNavigationItemSelectedListener(this)
        bottomNav_truthordare.selectedItemId = R.id.action_game
        if (savedInstanceState != null) {
            return
        }
        firstFragment.arguments = intent.extras
        val transaction = supportFragmentManager.beginTransaction()
        transaction.add(R.id.fragment_layout, GameFragment())
        transaction.add(R.id.fragment_layout, LeaderboardsFragment())
        transaction.add(R.id.fragment_layout, OptionsFragment())
        transaction.commit()

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.hide(GameFragment())
        transaction.hide(OptionsFragment())
        transaction.commit()

        return true
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_truthordare"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    tools:context=".TruthOrDare">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_truthordare"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimaryDark"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@color/colorPrimaryDark"
        app:title="Level 1" />

    <FrameLayout
        android:id="@+id/fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomNav_truthordare" android:layout_below="@id/toolbar_truthordare">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNav_truthordare"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        app:itemIconTint="@color/bottom_nav_items"
        app:itemTextColor="@color/bottom_nav_items"
        android:layout_alignParentBottom="true"
        app:menu="@menu/menu" />

</RelativeLayout>


有人能告诉我为什么
hide()
不起作用吗?

您试图隐藏不同于在
onCreate()
中创建和处理的实例。请保留对您在
onCreate()
中创建的对象的引用,或将标记添加到它们的事务中,然后使用
FragmentManager\findFragmentByTag()
将它们放入
onNavigationItemSelected()
。谢谢
transaction.add(R.id.fragment\u布局,GameFragment(),“add\u game”)
val game=supportFragmentManager.findFragmentByTag(“add\u game”)
工作正常。您试图隐藏的实例与在
onCreate()
中创建和处理的实例不同。请保留对您在
onCreate()
中创建的对象的引用,或将标记添加到它们的事务中,然后使用
FragmentManager\findFragmentByTag()
将它们放入
onNavigationItemSelected()
。谢谢
transaction.add(R.id.fragment\u布局,GameFragment(),“add\u game”)
val game=supportFragmentManager.findFragmentByTag(“add\u game”)
效果完美。