gpt4 book ai didi

android - 管理 fragment 事务时优化 Android 中的内存

转载 作者:行者123 更新时间:2023-11-29 01:29:49 29 4
gpt4 key购买 nike

我在我的应用程序中使用 fragment ,其中所有 fragment 都共享一个基本 Activity ,因此在应用程序关闭之前 Activity 不会被销毁。当我从一个 fragment 导航到另一个 fragment 时,我使用了以下代码:

 Fragment myFragment=new MyFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().addToBackStack("MyFragment")
.replace(R.id.content_frame, myFragment,"AirtimeFragment").commit();

使用它时,我必须为每个 fragment 创建一个新的 Object,这不利于内存消耗。

所以我想创建一个管理所有 fragment 的全局方法,它检查 fragment 是否已经存在,然后不需要重新创建它只使用现有的 fragment 对象,所以我可以减少内存消耗?

最佳答案

FragmentTransaction 可以使用这个方法来管理

private void FragmentReplace(Class fragmentClass, Bundle bundle,Boolean isAddToBackStack) {
try {

Fragment fragment;
Bundle bundle1Local;

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

System.out.println("Class Name of Fragment is String ::= " + fragmentClass.getName());
String fragmentTag=fragmentClass.getName();

fragment=fragmentManager.findFragmentByTag(fragmentTag);

if (fragment == null) {

System.out.println("Fragment is null, generating new "+fragmentTag+" fragment ");

/**
* Creating Fragment Object But Please Remember Fragment Must Have Empty Constrctor and it is Public
*/

Constructor constructor = fragmentClass.getConstructor();
Object instanceFragment = constructor.newInstance();

fragment=(Fragment) instanceFragment;
if(bundle != null) {
fragment.setArguments(bundle);
}
if(isAddToBackStack)
fragmentTransaction.addToBackStack(fragmentTag);

fragmentTransaction.replace(R.id.content_frame, fragment, fragmentTag);
fragmentTransaction.commit();

} else if (fragment.isAdded()) {

System.out.println("Fragment is added " + fragmentTag + " fragment ");

bundle1Local=fragment.getArguments();
if(bundle1Local != null)
fragment.getArguments().clear();

if(bundle != null) {
fragment.getArguments().putAll(bundle);
}
if(isAddToBackStack)
fragmentTransaction.addToBackStack(fragmentTag);

fragmentTransaction.show(fragment);

} else {

System.out.println("Fragment is not added but still there " + fragmentTag + " fragment ");

fragmentTransaction.detach(fragment);
System.out.println("Fragment is detached " + fragmentTag + " fragment ");

System.out.println("Fragment is null, generating new " + fragmentTag + " fragment ");
Constructor constructor = fragmentClass.getConstructor();
Object instanceFragment = constructor.newInstance();

fragment=(Fragment) instanceFragment;
if(bundle != null) {
fragment.setArguments(bundle);
}
if(isAddToBackStack)
fragmentTransaction.addToBackStack(fragmentTag);

fragmentTransaction.replace(R.id.content_frame, fragment, fragmentTag);
fragmentTransaction.commit();

}

} catch (Exception ex) {

System.out.println("Error::" + ex.toString());

}

}

关于android - 管理 fragment 事务时优化 Android 中的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31806873/

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