gpt4 book ai didi

android - 检查 fragment 是否在 framelayout 中显示或不在 android 中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:24:24 26 4
gpt4 key购买 nike

嗨, friend 们,我在 movies.xml 中有两个框架布局,即 containerdetail_screen。在容器中将添加包含 listview 的 movies.xml,并且在 detail_screen 中将具有名为 movie_details.xml 的可扩展 ListView 。现在想以编程方式检查 detail_screen 是否已经呈现 fragment 。如果呈现只是想删除该 fragment 。我通过 follwing 做了它并且工作正常。

    if (getSupportFragmentManager().findFragmentByTag("MyFragment") != null) {
getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentByTag("MyFragment")).commit();
}

但是有没有其他方法可以通过编程方式在 android 的框架布局中查找 fragment 是否显示。提前致谢

最佳答案

我创建的解决方案如下。即在将任何 fragment 添加到 details_screen 之前,只需检查是否已经显示 fragment 然后弹出它,然后添加新 fragment 。

if (getSupportFragmentManager().findFragmentById(R.id.detail_screen) != null) {

getSupportFragmentManager().popBackStack();

} //to do add fragment

希望它能奏效。

关于android - 检查 fragment 是否在 framelayout 中显示或不在 android 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18445264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com