我已尝试使用commitNow
以确保片段事务是同步的。但是,在测试之后,我发现由于某些原因commitNow
不能立即删除碎片。
我添加了一个具有postDelayed 100毫秒的Handler,并且该片段仍然不为null。之后,我将postDelayed
更改为1000毫秒,最后,该片段为空。
如何在调用commitNow
之后确保该片段立即为空?我试图添加supportFragmentManager.popBackStack()
,但没有帮助。
如果不可能立即使片段为空,那么在添加相同片段之前,如何检查该片段是否不再在堆栈中?
UPDATE我发现如果删除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)
}
}
为此,您必须使用popBackStackImmediate()
。更多信息here