【问题标题】:Why commitNow does not set fragment to null?为什么commitNow 不将片段设置为null?
【发布时间】:2020-01-29 15:48:54
【问题描述】:

我尝试使用commitNow 来确保片段事务是同步的。但是,经过我的测试,我发现由于某种原因commitNow 不会立即删除碎片。

我添加了一个 postDelayed 100 毫秒的处理程序,并且片段仍然不为空。之后,我将postDelayed 更改为 1000 毫秒,最后片段为空。

在我调用commitNow 后,如何确保片段立即为空?我尝试添加supportFragmentManager.popBackStack(),但没有任何帮助。

如果无法立即使片段为空,我如何在添加相同片段之前检查该片段是否不再在堆栈中?

更新 我发现如果我删除了setCustomAnimations,那么交易确实会立即发生。 我如何观察setCustomAnimations 何时完成?

val fragment = supportFragmentManager.findFragmentByTag(DEMO_FRAGMENT)
    if (fragment != null) {
        Log.d("test", "DEMO_FRAGMENT is not null")
        val fragmentTransaction = supportFragmentManager.beginTransaction()
            fragmentTransaction.setCustomAnimations(R.anim.custom_fade_in, R.anim.custom_fade_out)
                    .remove(fragment).commitNow()
            supportFragmentManager.popBackStack();
            Log.d("test", "is fragment null:${supportFragmentManager.findFragmentByTag(DEMO_FRAGMENT) == null}")
            Handler().postDelayed({
                Log.d("test", "is fragment null:${supportFragmentManager.findFragmentByTag(DEMO_FRAGMENT) == null}")
            }, 1000)
        } 
    }

【问题讨论】:

    标签: android android-fragments fragmenttransaction


    【解决方案1】:

    为此,您必须使用popBackStackImmediate()。更多信息here

    【讨论】:

      猜你喜欢
      • 2010-10-16
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2013-05-09
      相关资源
      最近更新 更多