我想在调用给定回调时向用户显示一个 DialogFragment。我的问题是有时同时对监听器进行多次调用,并且 fragment 管理器显示多个 fragment ,我正在使用以下代码
if (getSupportFragmentManager().findFragmentByTag("testFragment") == null) {
getSupportFragmentManager().beginTransaction()
.add(new MyFragment(), "testFragment")
.commit();
Log.e("TAG", "Show! "+(getSupportFragmentManager()
.findFragmentByTag("testFragment") == null));
}
正如最后一行中的日志消息所示,在提交 FragmentTransaction 后,findFragmentByTag 会在短时间内返回 null。那么,有什么方法可以确保只显示一次消息,而不是保存最后一次提交调用的 mili-time 并在一秒钟内忽略以后的调用?
Best Answer-推荐答案 strong>
在提交后立即调用 getSupportFragmentManager().executePendingTransactions() 应该强制 fragment 管理器立即提交 fragment ,以便后续调用 findFragmentByTag("testFragment") 不应再返回 null。
关于android - 一次且仅显示一次 DialogFragment,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/12355196/
|