android – 删除片段然后重新添加它

android – 删除片段然后重新添加它,第1张

概述我在我的活动中编写了一个方法setLoading,负责设置我的应用程序的“加载”状态.此方法负责实例化一个LoadingFragment,删除它的任何现有实例(使用FragmentManager),然后根据它的第一个参数加载,将其添加到两个可能的容器之一(取决于top参数). protected LoadingFragment loadingFragment;public void setLo 我在我的活动中编写了一个方法setLoading,负责设置我的应用程序的“加载”状态.此方法负责实例化一个LoadingFragment,删除它的任何现有实例(使用FragmentManager),然后根据它的第一个参数加载,将其添加到两个可能的容器之一(取决于top参数).

protected LoadingFragment loadingFragment;public voID setLoading(boolean loading,boolean top) {    FragmentManager fragmentManager = getFragmentManager();    // Create LoadingFragment instance if it has not already been created    if (loadingFragment == null || !(loadingFragment instanceof LoadingFragment)) {        loadingFragment = new LoadingFragment();    }    // Remove the fragment first if it is present    fragmentManager            .beginTransaction()            .remove(loadingFragment)            .commit();    // Only if loading is true should we display the fragment    if (loading) {        // DecIDe which container we're going to put the fragment in        int ID = top ? R.ID.topContainer : R.ID.container;        // Place the fragment in the right position        fragmentManager                .beginTransaction()                .add(ID,loadingFragment)                .commit();    }}public voID setLoading(boolean loading) {    setLoading(loading,true);}

我从我的活动中的其他地方触发setLoading(true),并且在测试时我已经注释掉了相应的setLoading(false).

我想要发生的是每次调用setLoading(true)时我的LoadingFragment都会出现.第一个调用不应该删除任何东西,因为它在那时它不存在.所有后续调用都应删除现有的LoadingFragment并再次添加.

发生的事情是第一次调用setLoading(true)确实创建了LoadingFragment并将其放在正确的容器中.但是,后续调用setLoading(true)会删除片段,但似乎永远不会重新添加.我已经检查过该片段确实存在,并且在添加它的时候是LoadingFragment类型,我还检查过以确保它正在调用onCreateVIEw方法.

难道我做错了什么?

编辑

使用H Raval下面给出的答案作为基础我现在提出以下内容:

public voID setLoading(boolean loading,boolean top) {    FragmentManager fragmentManager = getFragmentManager();    Fragment currentLoadingFragment = fragmentManager.findFragmentByID(R.ID.loadingFragment);    if (currentLoadingFragment != null) {        fragmentManager                .beginTransaction()                .remove(currentLoadingFragment)                .commit();    }    if (loading) {        int ID = top ? R.ID.topContainer : R.ID.container;        fragmentManager                .beginTransaction()                .add(ID,new LoadingFragment())                .commit();    }}

这似乎按预期工作.似乎主要区别在于此代码每次都创建一个新的LoadingFragment实例(当load = true时),而最初我试图使用相同的实例,只是使用FragmentManager添加/删除它.

出于兴趣,我有必要在使用remove后创建一个新实例吗?这是正确的方法吗?或者它应该在使用相同的实例时仍然有用吗?另外,如果建议每次都创建一个新实例,那么在清理,释放资源等方面我应该做些什么(也许有一种方法可以优雅地破坏过时的实例)?

解决方法 好吧,我已经对你的代码做了一些改变,并且对我来说非常适合.如果你遇到任何困难,我知道

public voID loadFragment(boolean loading,boolean top){        FragmentManager fragmentManager = getSupportFragmentManager();          loadingFragment = new LoadingFragment();        // Only if loading is true should we display the fragment        if (loading) {            // DecIDe which container we're going to put the fragment in            int ID = top ? R.ID.topContainer : R.ID.container;            if(top){                if(fragmentManager.findFragmentByTag("loadingFragment")!=null)                    fragmentManager.beginTransaction().remove(fragmentManager.findFragmentByTag("loadingFragment")).commit();                fragmentManager                        .beginTransaction()                        .replace(R.ID.topContainer,loadingFragment,"toploadingFragment")                        .commit();            }else{                if(fragmentManager.findFragmentByTag("toploadingFragment")!=null)                    fragmentManager.beginTransaction().remove(fragmentManager.findFragmentByTag("toploadingFragment")).commit();                fragmentManager                        .beginTransaction()                        .replace(R.ID.container,"loadingFragment")                        .commit();            }        }
总结

以上是内存溢出为你收集整理的android – 删除片段然后重新添加它全部内容,希望文章能够帮你解决android – 删除片段然后重新添加它所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1125738.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存