管理片段事务时优化Android中的内存

时间:2015-08-04 10:29:46

标签: android android-fragments memory-management

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

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

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

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

2 个答案:

答案 0 :(得分:1)

您可以使用此方法进行片段事务管理

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());

    }

}

答案 1 :(得分:0)

尝试这种方式https://github.com/rathodchintan/Fragment-Back-Stack

你可以创建堆栈变量作为公共静态,然后检查它是否存在...