Helper class for Fragments management











up vote
2
down vote

favorite












A Helper class for Fragment Management.

The FragmentsManager class has methods to add and replace fragments.

The replace method checks if the given fragment is present in backstack or not and if the fragment is present it brings back the old fragment.
Need review for the following:




  • Code optimized.

  • Memory Efficiency

  • Overall code review

  • Can the replace method be improved in any way so as to avoid creation of additional fragments?

  • I've tried to use popbackstack() but couldn't how to use it. Can you help with that?


Thanks



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 {

private static final String TAG = "FragmentsManager";
private static FragmentTransaction transaction;
// Return Fragment Transaction
private static FragmentTransaction getTransaction(Activity activity){
// if (transaction == null) {
// return transaction;
// }
return getFragmentManager(activity).beginTransaction();
}
// Return Fragment Manager
private static FragmentManager getFragmentManager(Activity activity){
return ((AppCompatActivity)activity).getSupportFragmentManager();
}

/**
* Add Fragment to the given ID
* @param activity
* @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();
}
else{
transaction = getTransaction(activity);
transaction.replace(id,check_Fragment,check_Fragment.getClass().getName())
.addToBackStack(null)
.commit();
}
}
}









share|improve this question









New contributor




sdb1995 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite












    A Helper class for Fragment Management.

    The FragmentsManager class has methods to add and replace fragments.

    The replace method checks if the given fragment is present in backstack or not and if the fragment is present it brings back the old fragment.
    Need review for the following:




    • Code optimized.

    • Memory Efficiency

    • Overall code review

    • Can the replace method be improved in any way so as to avoid creation of additional fragments?

    • I've tried to use popbackstack() but couldn't how to use it. Can you help with that?


    Thanks



    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 {

    private static final String TAG = "FragmentsManager";
    private static FragmentTransaction transaction;
    // Return Fragment Transaction
    private static FragmentTransaction getTransaction(Activity activity){
    // if (transaction == null) {
    // return transaction;
    // }
    return getFragmentManager(activity).beginTransaction();
    }
    // Return Fragment Manager
    private static FragmentManager getFragmentManager(Activity activity){
    return ((AppCompatActivity)activity).getSupportFragmentManager();
    }

    /**
    * Add Fragment to the given ID
    * @param activity
    * @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();
    }
    else{
    transaction = getTransaction(activity);
    transaction.replace(id,check_Fragment,check_Fragment.getClass().getName())
    .addToBackStack(null)
    .commit();
    }
    }
    }









    share|improve this question









    New contributor




    sdb1995 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      A Helper class for Fragment Management.

      The FragmentsManager class has methods to add and replace fragments.

      The replace method checks if the given fragment is present in backstack or not and if the fragment is present it brings back the old fragment.
      Need review for the following:




      • Code optimized.

      • Memory Efficiency

      • Overall code review

      • Can the replace method be improved in any way so as to avoid creation of additional fragments?

      • I've tried to use popbackstack() but couldn't how to use it. Can you help with that?


      Thanks



      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 {

      private static final String TAG = "FragmentsManager";
      private static FragmentTransaction transaction;
      // Return Fragment Transaction
      private static FragmentTransaction getTransaction(Activity activity){
      // if (transaction == null) {
      // return transaction;
      // }
      return getFragmentManager(activity).beginTransaction();
      }
      // Return Fragment Manager
      private static FragmentManager getFragmentManager(Activity activity){
      return ((AppCompatActivity)activity).getSupportFragmentManager();
      }

      /**
      * Add Fragment to the given ID
      * @param activity
      * @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();
      }
      else{
      transaction = getTransaction(activity);
      transaction.replace(id,check_Fragment,check_Fragment.getClass().getName())
      .addToBackStack(null)
      .commit();
      }
      }
      }









      share|improve this question









      New contributor




      sdb1995 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      A Helper class for Fragment Management.

      The FragmentsManager class has methods to add and replace fragments.

      The replace method checks if the given fragment is present in backstack or not and if the fragment is present it brings back the old fragment.
      Need review for the following:




      • Code optimized.

      • Memory Efficiency

      • Overall code review

      • Can the replace method be improved in any way so as to avoid creation of additional fragments?

      • I've tried to use popbackstack() but couldn't how to use it. Can you help with that?


      Thanks



      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 {

      private static final String TAG = "FragmentsManager";
      private static FragmentTransaction transaction;
      // Return Fragment Transaction
      private static FragmentTransaction getTransaction(Activity activity){
      // if (transaction == null) {
      // return transaction;
      // }
      return getFragmentManager(activity).beginTransaction();
      }
      // Return Fragment Manager
      private static FragmentManager getFragmentManager(Activity activity){
      return ((AppCompatActivity)activity).getSupportFragmentManager();
      }

      /**
      * Add Fragment to the given ID
      * @param activity
      * @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();
      }
      else{
      transaction = getTransaction(activity);
      transaction.replace(id,check_Fragment,check_Fragment.getClass().getName())
      .addToBackStack(null)
      .commit();
      }
      }
      }






      java android






      share|improve this question









      New contributor




      sdb1995 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      sdb1995 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 days ago









      200_success

      127k15148411




      127k15148411






      New contributor




      sdb1995 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      sdb1995

      112




      112




      New contributor




      sdb1995 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      sdb1995 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      sdb1995 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          your code looks very good, there are only minor issues:



          1) naming



          since you don't create any instances of FragmentsManager you should consider to rename it to FragmentsUtility. An Utility class provides methods to help you with your code while a Manager is an instance that does the work for you.



          boolean should beginn with prefix is (or rarely as an alternative has/can/should) (in Java it's convention to use camelCase)
          so rename your boolean add_to_backstack into boolean isAddedToBackstack rather (btw. in java it's convention to use camelCase)



          2) comments



          remove commented code!



          3) javadoc



          you provide some javadoc on the addFragment methode which is very crude - get things done and finish it!



          the same applies to replaceFragment.






          share|improve this answer























          • Thank you for your review. I thought JavaDocs should be provided for all the methods defined for description purposes. So I added them. Guess I was wrong. Thanks again.
            – sdb1995
            21 hours ago













          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          sdb1995 is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208219%2fhelper-class-for-fragments-management%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          your code looks very good, there are only minor issues:



          1) naming



          since you don't create any instances of FragmentsManager you should consider to rename it to FragmentsUtility. An Utility class provides methods to help you with your code while a Manager is an instance that does the work for you.



          boolean should beginn with prefix is (or rarely as an alternative has/can/should) (in Java it's convention to use camelCase)
          so rename your boolean add_to_backstack into boolean isAddedToBackstack rather (btw. in java it's convention to use camelCase)



          2) comments



          remove commented code!



          3) javadoc



          you provide some javadoc on the addFragment methode which is very crude - get things done and finish it!



          the same applies to replaceFragment.






          share|improve this answer























          • Thank you for your review. I thought JavaDocs should be provided for all the methods defined for description purposes. So I added them. Guess I was wrong. Thanks again.
            – sdb1995
            21 hours ago

















          up vote
          0
          down vote













          your code looks very good, there are only minor issues:



          1) naming



          since you don't create any instances of FragmentsManager you should consider to rename it to FragmentsUtility. An Utility class provides methods to help you with your code while a Manager is an instance that does the work for you.



          boolean should beginn with prefix is (or rarely as an alternative has/can/should) (in Java it's convention to use camelCase)
          so rename your boolean add_to_backstack into boolean isAddedToBackstack rather (btw. in java it's convention to use camelCase)



          2) comments



          remove commented code!



          3) javadoc



          you provide some javadoc on the addFragment methode which is very crude - get things done and finish it!



          the same applies to replaceFragment.






          share|improve this answer























          • Thank you for your review. I thought JavaDocs should be provided for all the methods defined for description purposes. So I added them. Guess I was wrong. Thanks again.
            – sdb1995
            21 hours ago















          up vote
          0
          down vote










          up vote
          0
          down vote









          your code looks very good, there are only minor issues:



          1) naming



          since you don't create any instances of FragmentsManager you should consider to rename it to FragmentsUtility. An Utility class provides methods to help you with your code while a Manager is an instance that does the work for you.



          boolean should beginn with prefix is (or rarely as an alternative has/can/should) (in Java it's convention to use camelCase)
          so rename your boolean add_to_backstack into boolean isAddedToBackstack rather (btw. in java it's convention to use camelCase)



          2) comments



          remove commented code!



          3) javadoc



          you provide some javadoc on the addFragment methode which is very crude - get things done and finish it!



          the same applies to replaceFragment.






          share|improve this answer














          your code looks very good, there are only minor issues:



          1) naming



          since you don't create any instances of FragmentsManager you should consider to rename it to FragmentsUtility. An Utility class provides methods to help you with your code while a Manager is an instance that does the work for you.



          boolean should beginn with prefix is (or rarely as an alternative has/can/should) (in Java it's convention to use camelCase)
          so rename your boolean add_to_backstack into boolean isAddedToBackstack rather (btw. in java it's convention to use camelCase)



          2) comments



          remove commented code!



          3) javadoc



          you provide some javadoc on the addFragment methode which is very crude - get things done and finish it!



          the same applies to replaceFragment.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Martin Frank

          547315




          547315












          • Thank you for your review. I thought JavaDocs should be provided for all the methods defined for description purposes. So I added them. Guess I was wrong. Thanks again.
            – sdb1995
            21 hours ago




















          • Thank you for your review. I thought JavaDocs should be provided for all the methods defined for description purposes. So I added them. Guess I was wrong. Thanks again.
            – sdb1995
            21 hours ago


















          Thank you for your review. I thought JavaDocs should be provided for all the methods defined for description purposes. So I added them. Guess I was wrong. Thanks again.
          – sdb1995
          21 hours ago






          Thank you for your review. I thought JavaDocs should be provided for all the methods defined for description purposes. So I added them. Guess I was wrong. Thanks again.
          – sdb1995
          21 hours ago












          sdb1995 is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          sdb1995 is a new contributor. Be nice, and check out our Code of Conduct.













          sdb1995 is a new contributor. Be nice, and check out our Code of Conduct.












          sdb1995 is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208219%2fhelper-class-for-fragments-management%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Quarter-circle Tiles

          build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

          Mont Emei