2

I'm using Fragments in my app where all of them share one base activity, so activity is not destroyed till app closes. When I navigate from one fragment to another, I have used the following code:

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

When using it I have to create a new Object for each fragment which is not good for memory consumption.

So I want to create one global method which manages all fragment in which it checks whether the fragment already exists, then no need to recreate it just uses the existing fragment object, so I can reduce memory consumption?

Machado
  • 14,105
  • 13
  • 56
  • 97

2 Answers2

1

you can us this method for fragment transaction to manage

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

    }

}
Mayur Patel
  • 103
  • 1
  • 2
  • 9
0

Try this way https://github.com/rathodchintan/Fragment-Back-Stack

you can create stack variable as public static and then check it is exist or not...

MPG
  • 785
  • 6
  • 20