Android – Help class for managing fragments

A class of help for managing fragments.
the Fragment manager The class has methods to add and replace fragments.
The replacement method checks whether the given fragment is present in backstack or not, and if the fragment is present, it returns the previous fragment.
Needs revision for the following:

  • Optimized code
  • Efficiency of memory
  • General revision of the code
  • Can the replacement method be improved in some way to avoid the creation of additional fragments?
  • I tried using popbackstack () but I could not use it. Can you help with that?

Thank you

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

class FragmentsManager {

static static end String TAG = "FragmentsManager";
static transaction private FragmentTransaction;
// Transaction of the return fragment
FragmentTransaction getTransaction (activity activity) static private {
// yes (transaction == null) {
// return transaction;
//}
returns getFragmentManager (activity) .beginTransaction ();
}
// Return fragment manager
Private static FragmentManager getFragmentManager (activity activity) {
Return activity ((AppCompatActivity)) .getSupportFragmentManager ();
}

/ **
* Add fragment to the given ID
* activity @param
* @param fragment
* @param id
* @param add_to_backstack
* /
static void addFragment (activity activity, fragment fragment, int id, boolean add_to_backstack) {
transaction = getTransaction (activity);
transaction.add (id, fragment, fragment.getClass (). getName ());
if (add_to_backstack)
transaction.addToBackStack (fragment.getClass (). getName ());
transaction.commit ();
}


static void replaceFragment (activity activity, fragment fragment, int id, boolean add_to_backstack) {
Fragment check_Fragment = getFragmentManager (activity) .findFragmentByTag (fragment.getClass (). GetName ());
if (check_Fragment == null) {
transaction = getTransaction (activity)
.replace (id, fragment, fragment.getClass (). getName ());
if (add_to_backstack)
transaction.addToBackStack (fragment.getClass (). getName ());
transaction.commit ();
}
plus{
transaction = getTransaction (activity);
transaction.replace (id, check_Fragment, check_Fragment.getClass (). getName ())
.addToBackStack (null)
.commit();
}
}
}