How to call onResume() of a child Fragment and not onResume() of the parent Fragment?












1















Suppose I have 3 Fragments, BaseFragment, and first fragment and second fragment.



I have implemented onResume() of BaseFragment and written a code to set the width and height like -



public void onResume() {

super.onResume();
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
getDialog().getWindow().setAttributes(params);

Log.d("upisdk", "basedialogfragment onresume()");
}


I have a first fragment, in which I want to set the width to, for example, 90% of the total width. I have implemented onResume() here also -



@Override
public void onResume() {
super.onResume();

WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = (getWidth(getContext()) / 100) * 90;
params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
getDialog().getWindow().setAttributes(params);

Log.d("upisdk", "onresume called on Changeddialogfragment");

}


And in SecondFragment I want to set to 80% I did this just like the above Fragment.



My onResume() is getting called twice. Once for BaseFragment and the other time for FirstFragment.



is this possible to call only first fragment onResume and not the parent one?










share|improve this question





























    1















    Suppose I have 3 Fragments, BaseFragment, and first fragment and second fragment.



    I have implemented onResume() of BaseFragment and written a code to set the width and height like -



    public void onResume() {

    super.onResume();
    WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
    params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
    params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
    params.gravity = Gravity.CENTER;
    getDialog().getWindow().setAttributes(params);

    Log.d("upisdk", "basedialogfragment onresume()");
    }


    I have a first fragment, in which I want to set the width to, for example, 90% of the total width. I have implemented onResume() here also -



    @Override
    public void onResume() {
    super.onResume();

    WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
    params.width = (getWidth(getContext()) / 100) * 90;
    params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
    params.gravity = Gravity.CENTER;
    getDialog().getWindow().setAttributes(params);

    Log.d("upisdk", "onresume called on Changeddialogfragment");

    }


    And in SecondFragment I want to set to 80% I did this just like the above Fragment.



    My onResume() is getting called twice. Once for BaseFragment and the other time for FirstFragment.



    is this possible to call only first fragment onResume and not the parent one?










    share|improve this question



























      1












      1








      1








      Suppose I have 3 Fragments, BaseFragment, and first fragment and second fragment.



      I have implemented onResume() of BaseFragment and written a code to set the width and height like -



      public void onResume() {

      super.onResume();
      WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
      params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
      params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
      params.gravity = Gravity.CENTER;
      getDialog().getWindow().setAttributes(params);

      Log.d("upisdk", "basedialogfragment onresume()");
      }


      I have a first fragment, in which I want to set the width to, for example, 90% of the total width. I have implemented onResume() here also -



      @Override
      public void onResume() {
      super.onResume();

      WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
      params.width = (getWidth(getContext()) / 100) * 90;
      params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
      params.gravity = Gravity.CENTER;
      getDialog().getWindow().setAttributes(params);

      Log.d("upisdk", "onresume called on Changeddialogfragment");

      }


      And in SecondFragment I want to set to 80% I did this just like the above Fragment.



      My onResume() is getting called twice. Once for BaseFragment and the other time for FirstFragment.



      is this possible to call only first fragment onResume and not the parent one?










      share|improve this question
















      Suppose I have 3 Fragments, BaseFragment, and first fragment and second fragment.



      I have implemented onResume() of BaseFragment and written a code to set the width and height like -



      public void onResume() {

      super.onResume();
      WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
      params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
      params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
      params.gravity = Gravity.CENTER;
      getDialog().getWindow().setAttributes(params);

      Log.d("upisdk", "basedialogfragment onresume()");
      }


      I have a first fragment, in which I want to set the width to, for example, 90% of the total width. I have implemented onResume() here also -



      @Override
      public void onResume() {
      super.onResume();

      WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
      params.width = (getWidth(getContext()) / 100) * 90;
      params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
      params.gravity = Gravity.CENTER;
      getDialog().getWindow().setAttributes(params);

      Log.d("upisdk", "onresume called on Changeddialogfragment");

      }


      And in SecondFragment I want to set to 80% I did this just like the above Fragment.



      My onResume() is getting called twice. Once for BaseFragment and the other time for FirstFragment.



      is this possible to call only first fragment onResume and not the parent one?







      android fragment






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 20:51









      0X0nosugar

      7,41331842




      7,41331842










      asked Nov 23 '18 at 20:17









      SniperSniper

      84021129




      84021129
























          1 Answer
          1






          active

          oldest

          votes


















          1















          is this possible to call only first fragment onResume and not the parent one?




          No, it isn't possible. This is what I get when I override onResume() in a Fragment and delete the line with super.onResume();




          java.lang.RuntimeException: Unable to resume activity {com.example.mytestapp/com.example.mytestapp.MainActivity}: android.support.v4.app.SuperNotCalledException: Fragment MyTestFragment{4037a6b #0 id=0x7f090035 fragment} did not call through to super.onResume()




          [...]




          Caused by: android.support.v4.app.SuperNotCalledException: Fragment
          MyTestFragment{4037a6b #0 id=0x7f090035 fragment} did not call
          through to super.onResume()




          But you can introduce a field protected int percent = x; in BaseFragment and let it have a different value x for each Fragment.



          You can set this value in any method which gets called before onResume() and does not have to call through to its superclass implementation, e.g. in onCreateView().



          Or you introduce your own method and call it from e.g. onCreate()



          For example for the parent class:



          protected void setPercent(){
          percent = 100;
          }

          @Override
          public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setPercent();
          }


          Whereas in the first Fragment, you write



          @Override
          protected void setPercent(){
          percent = 90;
          }


          Then the child Fragments don't need to override onResume() and the parent class is able to set the LayoutParams correctly:



          public void onResume() {
          super.onResume();
          WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
          params.width = (getWidth(getContext()) * percent / 100);
          params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
          params.gravity = Gravity.CENTER;
          getDialog().getWindow().setAttributes(params);
          }





          share|improve this answer


























          • So, for the first fragment, I have set the percent value. i need to pass the value of the percent to the BaseFragment or where my onResume is overrided?

            – Sniper
            Nov 23 '18 at 20:54











          • @Aman Verma - you can set the value for the field as part of the child Fragment's code like this: protected int percent = 90; This way, percent will have the correct value once the Fragment's constructor has finished. So in onResume(), one formula is able to cover all cases

            – 0X0nosugar
            Nov 23 '18 at 20:59











          • BaseFragment and the FirstFragment are two different classes. Are you aware of that??

            – Sniper
            Nov 23 '18 at 21:01











          • @Aman Verma - yup. You said FirstFragment extends from BaseFragment, didn't you?

            – 0X0nosugar
            Nov 23 '18 at 21:02











          • @Aman Verma - ok, I think there is one mistake in my approach. Which can be amended by overriding any method whch gets called before onResume()... will edit my answer

            – 0X0nosugar
            Nov 23 '18 at 21:04











          Your Answer






          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: "1"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452511%2fhow-to-call-onresume-of-a-child-fragment-and-not-onresume-of-the-parent-frag%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









          1















          is this possible to call only first fragment onResume and not the parent one?




          No, it isn't possible. This is what I get when I override onResume() in a Fragment and delete the line with super.onResume();




          java.lang.RuntimeException: Unable to resume activity {com.example.mytestapp/com.example.mytestapp.MainActivity}: android.support.v4.app.SuperNotCalledException: Fragment MyTestFragment{4037a6b #0 id=0x7f090035 fragment} did not call through to super.onResume()




          [...]




          Caused by: android.support.v4.app.SuperNotCalledException: Fragment
          MyTestFragment{4037a6b #0 id=0x7f090035 fragment} did not call
          through to super.onResume()




          But you can introduce a field protected int percent = x; in BaseFragment and let it have a different value x for each Fragment.



          You can set this value in any method which gets called before onResume() and does not have to call through to its superclass implementation, e.g. in onCreateView().



          Or you introduce your own method and call it from e.g. onCreate()



          For example for the parent class:



          protected void setPercent(){
          percent = 100;
          }

          @Override
          public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setPercent();
          }


          Whereas in the first Fragment, you write



          @Override
          protected void setPercent(){
          percent = 90;
          }


          Then the child Fragments don't need to override onResume() and the parent class is able to set the LayoutParams correctly:



          public void onResume() {
          super.onResume();
          WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
          params.width = (getWidth(getContext()) * percent / 100);
          params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
          params.gravity = Gravity.CENTER;
          getDialog().getWindow().setAttributes(params);
          }





          share|improve this answer


























          • So, for the first fragment, I have set the percent value. i need to pass the value of the percent to the BaseFragment or where my onResume is overrided?

            – Sniper
            Nov 23 '18 at 20:54











          • @Aman Verma - you can set the value for the field as part of the child Fragment's code like this: protected int percent = 90; This way, percent will have the correct value once the Fragment's constructor has finished. So in onResume(), one formula is able to cover all cases

            – 0X0nosugar
            Nov 23 '18 at 20:59











          • BaseFragment and the FirstFragment are two different classes. Are you aware of that??

            – Sniper
            Nov 23 '18 at 21:01











          • @Aman Verma - yup. You said FirstFragment extends from BaseFragment, didn't you?

            – 0X0nosugar
            Nov 23 '18 at 21:02











          • @Aman Verma - ok, I think there is one mistake in my approach. Which can be amended by overriding any method whch gets called before onResume()... will edit my answer

            – 0X0nosugar
            Nov 23 '18 at 21:04
















          1















          is this possible to call only first fragment onResume and not the parent one?




          No, it isn't possible. This is what I get when I override onResume() in a Fragment and delete the line with super.onResume();




          java.lang.RuntimeException: Unable to resume activity {com.example.mytestapp/com.example.mytestapp.MainActivity}: android.support.v4.app.SuperNotCalledException: Fragment MyTestFragment{4037a6b #0 id=0x7f090035 fragment} did not call through to super.onResume()




          [...]




          Caused by: android.support.v4.app.SuperNotCalledException: Fragment
          MyTestFragment{4037a6b #0 id=0x7f090035 fragment} did not call
          through to super.onResume()




          But you can introduce a field protected int percent = x; in BaseFragment and let it have a different value x for each Fragment.



          You can set this value in any method which gets called before onResume() and does not have to call through to its superclass implementation, e.g. in onCreateView().



          Or you introduce your own method and call it from e.g. onCreate()



          For example for the parent class:



          protected void setPercent(){
          percent = 100;
          }

          @Override
          public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setPercent();
          }


          Whereas in the first Fragment, you write



          @Override
          protected void setPercent(){
          percent = 90;
          }


          Then the child Fragments don't need to override onResume() and the parent class is able to set the LayoutParams correctly:



          public void onResume() {
          super.onResume();
          WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
          params.width = (getWidth(getContext()) * percent / 100);
          params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
          params.gravity = Gravity.CENTER;
          getDialog().getWindow().setAttributes(params);
          }





          share|improve this answer


























          • So, for the first fragment, I have set the percent value. i need to pass the value of the percent to the BaseFragment or where my onResume is overrided?

            – Sniper
            Nov 23 '18 at 20:54











          • @Aman Verma - you can set the value for the field as part of the child Fragment's code like this: protected int percent = 90; This way, percent will have the correct value once the Fragment's constructor has finished. So in onResume(), one formula is able to cover all cases

            – 0X0nosugar
            Nov 23 '18 at 20:59











          • BaseFragment and the FirstFragment are two different classes. Are you aware of that??

            – Sniper
            Nov 23 '18 at 21:01











          • @Aman Verma - yup. You said FirstFragment extends from BaseFragment, didn't you?

            – 0X0nosugar
            Nov 23 '18 at 21:02











          • @Aman Verma - ok, I think there is one mistake in my approach. Which can be amended by overriding any method whch gets called before onResume()... will edit my answer

            – 0X0nosugar
            Nov 23 '18 at 21:04














          1












          1








          1








          is this possible to call only first fragment onResume and not the parent one?




          No, it isn't possible. This is what I get when I override onResume() in a Fragment and delete the line with super.onResume();




          java.lang.RuntimeException: Unable to resume activity {com.example.mytestapp/com.example.mytestapp.MainActivity}: android.support.v4.app.SuperNotCalledException: Fragment MyTestFragment{4037a6b #0 id=0x7f090035 fragment} did not call through to super.onResume()




          [...]




          Caused by: android.support.v4.app.SuperNotCalledException: Fragment
          MyTestFragment{4037a6b #0 id=0x7f090035 fragment} did not call
          through to super.onResume()




          But you can introduce a field protected int percent = x; in BaseFragment and let it have a different value x for each Fragment.



          You can set this value in any method which gets called before onResume() and does not have to call through to its superclass implementation, e.g. in onCreateView().



          Or you introduce your own method and call it from e.g. onCreate()



          For example for the parent class:



          protected void setPercent(){
          percent = 100;
          }

          @Override
          public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setPercent();
          }


          Whereas in the first Fragment, you write



          @Override
          protected void setPercent(){
          percent = 90;
          }


          Then the child Fragments don't need to override onResume() and the parent class is able to set the LayoutParams correctly:



          public void onResume() {
          super.onResume();
          WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
          params.width = (getWidth(getContext()) * percent / 100);
          params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
          params.gravity = Gravity.CENTER;
          getDialog().getWindow().setAttributes(params);
          }





          share|improve this answer
















          is this possible to call only first fragment onResume and not the parent one?




          No, it isn't possible. This is what I get when I override onResume() in a Fragment and delete the line with super.onResume();




          java.lang.RuntimeException: Unable to resume activity {com.example.mytestapp/com.example.mytestapp.MainActivity}: android.support.v4.app.SuperNotCalledException: Fragment MyTestFragment{4037a6b #0 id=0x7f090035 fragment} did not call through to super.onResume()




          [...]




          Caused by: android.support.v4.app.SuperNotCalledException: Fragment
          MyTestFragment{4037a6b #0 id=0x7f090035 fragment} did not call
          through to super.onResume()




          But you can introduce a field protected int percent = x; in BaseFragment and let it have a different value x for each Fragment.



          You can set this value in any method which gets called before onResume() and does not have to call through to its superclass implementation, e.g. in onCreateView().



          Or you introduce your own method and call it from e.g. onCreate()



          For example for the parent class:



          protected void setPercent(){
          percent = 100;
          }

          @Override
          public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setPercent();
          }


          Whereas in the first Fragment, you write



          @Override
          protected void setPercent(){
          percent = 90;
          }


          Then the child Fragments don't need to override onResume() and the parent class is able to set the LayoutParams correctly:



          public void onResume() {
          super.onResume();
          WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
          params.width = (getWidth(getContext()) * percent / 100);
          params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
          params.gravity = Gravity.CENTER;
          getDialog().getWindow().setAttributes(params);
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 21:23

























          answered Nov 23 '18 at 20:39









          0X0nosugar0X0nosugar

          7,41331842




          7,41331842













          • So, for the first fragment, I have set the percent value. i need to pass the value of the percent to the BaseFragment or where my onResume is overrided?

            – Sniper
            Nov 23 '18 at 20:54











          • @Aman Verma - you can set the value for the field as part of the child Fragment's code like this: protected int percent = 90; This way, percent will have the correct value once the Fragment's constructor has finished. So in onResume(), one formula is able to cover all cases

            – 0X0nosugar
            Nov 23 '18 at 20:59











          • BaseFragment and the FirstFragment are two different classes. Are you aware of that??

            – Sniper
            Nov 23 '18 at 21:01











          • @Aman Verma - yup. You said FirstFragment extends from BaseFragment, didn't you?

            – 0X0nosugar
            Nov 23 '18 at 21:02











          • @Aman Verma - ok, I think there is one mistake in my approach. Which can be amended by overriding any method whch gets called before onResume()... will edit my answer

            – 0X0nosugar
            Nov 23 '18 at 21:04



















          • So, for the first fragment, I have set the percent value. i need to pass the value of the percent to the BaseFragment or where my onResume is overrided?

            – Sniper
            Nov 23 '18 at 20:54











          • @Aman Verma - you can set the value for the field as part of the child Fragment's code like this: protected int percent = 90; This way, percent will have the correct value once the Fragment's constructor has finished. So in onResume(), one formula is able to cover all cases

            – 0X0nosugar
            Nov 23 '18 at 20:59











          • BaseFragment and the FirstFragment are two different classes. Are you aware of that??

            – Sniper
            Nov 23 '18 at 21:01











          • @Aman Verma - yup. You said FirstFragment extends from BaseFragment, didn't you?

            – 0X0nosugar
            Nov 23 '18 at 21:02











          • @Aman Verma - ok, I think there is one mistake in my approach. Which can be amended by overriding any method whch gets called before onResume()... will edit my answer

            – 0X0nosugar
            Nov 23 '18 at 21:04

















          So, for the first fragment, I have set the percent value. i need to pass the value of the percent to the BaseFragment or where my onResume is overrided?

          – Sniper
          Nov 23 '18 at 20:54





          So, for the first fragment, I have set the percent value. i need to pass the value of the percent to the BaseFragment or where my onResume is overrided?

          – Sniper
          Nov 23 '18 at 20:54













          @Aman Verma - you can set the value for the field as part of the child Fragment's code like this: protected int percent = 90; This way, percent will have the correct value once the Fragment's constructor has finished. So in onResume(), one formula is able to cover all cases

          – 0X0nosugar
          Nov 23 '18 at 20:59





          @Aman Verma - you can set the value for the field as part of the child Fragment's code like this: protected int percent = 90; This way, percent will have the correct value once the Fragment's constructor has finished. So in onResume(), one formula is able to cover all cases

          – 0X0nosugar
          Nov 23 '18 at 20:59













          BaseFragment and the FirstFragment are two different classes. Are you aware of that??

          – Sniper
          Nov 23 '18 at 21:01





          BaseFragment and the FirstFragment are two different classes. Are you aware of that??

          – Sniper
          Nov 23 '18 at 21:01













          @Aman Verma - yup. You said FirstFragment extends from BaseFragment, didn't you?

          – 0X0nosugar
          Nov 23 '18 at 21:02





          @Aman Verma - yup. You said FirstFragment extends from BaseFragment, didn't you?

          – 0X0nosugar
          Nov 23 '18 at 21:02













          @Aman Verma - ok, I think there is one mistake in my approach. Which can be amended by overriding any method whch gets called before onResume()... will edit my answer

          – 0X0nosugar
          Nov 23 '18 at 21:04





          @Aman Verma - ok, I think there is one mistake in my approach. Which can be amended by overriding any method whch gets called before onResume()... will edit my answer

          – 0X0nosugar
          Nov 23 '18 at 21:04




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452511%2fhow-to-call-onresume-of-a-child-fragment-and-not-onresume-of-the-parent-frag%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python