1

我试图在两个之间移动fragments而不重新创建它们,这样前一个片段中的数据就不会消失。

我试图在互联网上寻找答案并尝试了几个小时但没有成功。我查看了这些链接:

之后我show()hide()尝试了AddToBackStack()但没有成功

class MainActivity : AppCompatActivity(){

    private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        @Override
        when (item.itemId) {
            R.id.navigation_home -> {
                //replaceFragment(SignInFragment())
                supportFragmentManager.beginTransaction().hide(AllEventsFragment()).commit()
                supportFragmentManager.beginTransaction().show(SignInFragment()).commit()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_events -> {
                //replaceFragment(AllEventsFragment())
                supportFragmentManager.beginTransaction().hide(SignInFragment()).commit()
                supportFragmentManager.beginTransaction().show(AllEventsFragment()).commit()
                if (currentUser.isNotEmpty()) {
                    updateRecyclerView()
                    sign_in_error?.visibility = View.INVISIBLE
                }
                return@OnNavigationItemSelectedListener true
            }
        }
        return@OnNavigationItemSelectedListener false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        supportFragmentManager.beginTransaction().add(R.id.activity_main, AllEventsFragment(), "2").commit()
        supportFragmentManager.beginTransaction().add(R.id.activity_main, SignInFragment(), "1").commit()
        val navView: BottomNavigationView = findViewById(R.id.nav_view)
        navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
        personInEvent = false
    }

结果是重叠的片段没有真正在它们之间导航的选项。我真的尝试了我所知道的一切,互联网上有一些答案,但没有一个能帮助我解决我的问题。对于这个令人沮丧的问题,我真的很感激。

导航前: 导航前:

导航后: 导航后:

4

2 回答 2

2
supportFragmentManager.beginTransaction().hide(AllEventsFragment()).commit()

您每次都在重新创建您的片段!呼叫AllEventsFragment()相当于 new AllEventsFragment()

例如,您需要先实例化它们,您的代码需要是这样的,

    val fragment1: Fragment = SignInFragment()
    val fragment2: Fragment = AllEventsFragment()
    var active = fragment1

      override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            supportFragmentManager.beginTransaction().add(R.id.activity_main,fragment2 , "2").commit()
            supportFragmentManager.beginTransaction().add(R.id.activity_main, fragment1, "1").commit()
            val navView: BottomNavigationView = findViewById(R.id.nav_view)
            navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
            personInEvent = false
        }

在你的听众里面

    R.id.navigation_home -> {
                 supportFragmentManager.beginTransaction().beginTransaction().hide(active).show(fragment1).commit();
                    active = fragment1;
                return@OnNavigationItemSelectedListener true
            }
 R.id.navigation_events -> {
                //replaceFragment(AllEventsFragment())
                supportFragmentManager.beginTransaction().beginTransaction().hide(active).show(fragment2).commit();
active = fragment2
)

    //handle rest of the cases 
于 2019-05-18T23:25:13.137 回答
0

看看架构组件,你也可以用旧的Android 方式实现这一点 - 保存/恢复片段状态 当片段不可见时,它会暂停甚至可能被破坏,使用 bundle 来保存数据。您想要实现的目标可以使用两个容器来完成,但您真的不应该

于 2019-05-18T23:18:14.247 回答