commitAllowingStateLoss on DialogFragment











up vote
16
down vote

favorite
3












I have an IllegalStateException on showing a DialogFragment :



java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState


i know why its happening but i want to using commitAllowingStateLoss on showing dialog by overriding DialogFragment show function :



public void show(FragmentManager manager, String tag) {
mDismissed = false;
mShownByMe = true;
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commit(); //replace it by commitAllowingStateLoss
}


but i don't access to mDismissed and mShownByMe variables , how can i access those variables to modify them as it's parent did.










share|improve this question


























    up vote
    16
    down vote

    favorite
    3












    I have an IllegalStateException on showing a DialogFragment :



    java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState


    i know why its happening but i want to using commitAllowingStateLoss on showing dialog by overriding DialogFragment show function :



    public void show(FragmentManager manager, String tag) {
    mDismissed = false;
    mShownByMe = true;
    FragmentTransaction ft = manager.beginTransaction();
    ft.add(this, tag);
    ft.commit(); //replace it by commitAllowingStateLoss
    }


    but i don't access to mDismissed and mShownByMe variables , how can i access those variables to modify them as it's parent did.










    share|improve this question
























      up vote
      16
      down vote

      favorite
      3









      up vote
      16
      down vote

      favorite
      3






      3





      I have an IllegalStateException on showing a DialogFragment :



      java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState


      i know why its happening but i want to using commitAllowingStateLoss on showing dialog by overriding DialogFragment show function :



      public void show(FragmentManager manager, String tag) {
      mDismissed = false;
      mShownByMe = true;
      FragmentTransaction ft = manager.beginTransaction();
      ft.add(this, tag);
      ft.commit(); //replace it by commitAllowingStateLoss
      }


      but i don't access to mDismissed and mShownByMe variables , how can i access those variables to modify them as it's parent did.










      share|improve this question













      I have an IllegalStateException on showing a DialogFragment :



      java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState


      i know why its happening but i want to using commitAllowingStateLoss on showing dialog by overriding DialogFragment show function :



      public void show(FragmentManager manager, String tag) {
      mDismissed = false;
      mShownByMe = true;
      FragmentTransaction ft = manager.beginTransaction();
      ft.add(this, tag);
      ft.commit(); //replace it by commitAllowingStateLoss
      }


      but i don't access to mDismissed and mShownByMe variables , how can i access those variables to modify them as it's parent did.







      android android-fragments android-dialogfragment dialogfragment






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 24 '15 at 13:57









      Arash GM

      8,41444568




      8,41444568
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          31
          down vote



          accepted










          I think to prevent throwing IllegalStateException on DialogFragment might be better to use :



           YourDialogFragment dialogFragment = new YourDialogFragment ();
          fragmentManager.beginTransaction().add(dialogFragment, YourDialogFragment .TAG_FRAGMENT).commitAllowingStateLoss();


          instead of using show() on DialogFragment.






          share|improve this answer






























            up vote
            3
            down vote













            The solution about commitAllowingStateLoss works if your DialogFragment has no state to save, otherwise they will be lost like the function name told. But I think in most cases we have state to save, that is the major benefit of DialogFragment: Android recreate it and maintains its state automatically.



            A better solution would be to check if the recreate process done, if not then return to caller, which is either an Activity or a FragmentActivity, it should call mark it and call the show function again later in its onPostResume() or onResumeFragments() callback, which we can make sure all fragments are recreated.



            Here is an overridden show() from a subclass of DialogFragment:



            public boolean show(FragmentManager fragmentManager) {
            if (fragmentManager.isStateSaved()) return false;
            show(fragmentManager, tagName);
            return true;
            }





            share|improve this answer




























              up vote
              0
              down vote













              origin dialog fragment



              public void show(FragmentManager manager, String tag) {
              mDismissed = false;
              mShownByMe = true;
              FragmentTransaction ft = manager.beginTransaction();
              ft.add(this, tag);
              ft.commit(); //replace it by commitAllowingStateLoss
              }



              i don't know mDismissed, mShownByMe variables used for, so should be better if override show(FagmentManager, String) method of DialogFragment and it works fine with me



              override fun show(manager: FragmentManager?, tag: String?) {
              if (manager?.isDestroyed == false && !manager.isStateSaved) {
              super.show(manager, tag)
              }
              }



              isStateSaved available from appcompat >= 26.0.0 or androidx






              share|improve this answer























                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%2f30424319%2fcommitallowingstateloss-on-dialogfragment%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                31
                down vote



                accepted










                I think to prevent throwing IllegalStateException on DialogFragment might be better to use :



                 YourDialogFragment dialogFragment = new YourDialogFragment ();
                fragmentManager.beginTransaction().add(dialogFragment, YourDialogFragment .TAG_FRAGMENT).commitAllowingStateLoss();


                instead of using show() on DialogFragment.






                share|improve this answer



























                  up vote
                  31
                  down vote



                  accepted










                  I think to prevent throwing IllegalStateException on DialogFragment might be better to use :



                   YourDialogFragment dialogFragment = new YourDialogFragment ();
                  fragmentManager.beginTransaction().add(dialogFragment, YourDialogFragment .TAG_FRAGMENT).commitAllowingStateLoss();


                  instead of using show() on DialogFragment.






                  share|improve this answer

























                    up vote
                    31
                    down vote



                    accepted







                    up vote
                    31
                    down vote



                    accepted






                    I think to prevent throwing IllegalStateException on DialogFragment might be better to use :



                     YourDialogFragment dialogFragment = new YourDialogFragment ();
                    fragmentManager.beginTransaction().add(dialogFragment, YourDialogFragment .TAG_FRAGMENT).commitAllowingStateLoss();


                    instead of using show() on DialogFragment.






                    share|improve this answer














                    I think to prevent throwing IllegalStateException on DialogFragment might be better to use :



                     YourDialogFragment dialogFragment = new YourDialogFragment ();
                    fragmentManager.beginTransaction().add(dialogFragment, YourDialogFragment .TAG_FRAGMENT).commitAllowingStateLoss();


                    instead of using show() on DialogFragment.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 4 '17 at 12:39









                    Natali

                    2,65942747




                    2,65942747










                    answered May 25 '15 at 9:26









                    Arash GM

                    8,41444568




                    8,41444568
























                        up vote
                        3
                        down vote













                        The solution about commitAllowingStateLoss works if your DialogFragment has no state to save, otherwise they will be lost like the function name told. But I think in most cases we have state to save, that is the major benefit of DialogFragment: Android recreate it and maintains its state automatically.



                        A better solution would be to check if the recreate process done, if not then return to caller, which is either an Activity or a FragmentActivity, it should call mark it and call the show function again later in its onPostResume() or onResumeFragments() callback, which we can make sure all fragments are recreated.



                        Here is an overridden show() from a subclass of DialogFragment:



                        public boolean show(FragmentManager fragmentManager) {
                        if (fragmentManager.isStateSaved()) return false;
                        show(fragmentManager, tagName);
                        return true;
                        }





                        share|improve this answer

























                          up vote
                          3
                          down vote













                          The solution about commitAllowingStateLoss works if your DialogFragment has no state to save, otherwise they will be lost like the function name told. But I think in most cases we have state to save, that is the major benefit of DialogFragment: Android recreate it and maintains its state automatically.



                          A better solution would be to check if the recreate process done, if not then return to caller, which is either an Activity or a FragmentActivity, it should call mark it and call the show function again later in its onPostResume() or onResumeFragments() callback, which we can make sure all fragments are recreated.



                          Here is an overridden show() from a subclass of DialogFragment:



                          public boolean show(FragmentManager fragmentManager) {
                          if (fragmentManager.isStateSaved()) return false;
                          show(fragmentManager, tagName);
                          return true;
                          }





                          share|improve this answer























                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            The solution about commitAllowingStateLoss works if your DialogFragment has no state to save, otherwise they will be lost like the function name told. But I think in most cases we have state to save, that is the major benefit of DialogFragment: Android recreate it and maintains its state automatically.



                            A better solution would be to check if the recreate process done, if not then return to caller, which is either an Activity or a FragmentActivity, it should call mark it and call the show function again later in its onPostResume() or onResumeFragments() callback, which we can make sure all fragments are recreated.



                            Here is an overridden show() from a subclass of DialogFragment:



                            public boolean show(FragmentManager fragmentManager) {
                            if (fragmentManager.isStateSaved()) return false;
                            show(fragmentManager, tagName);
                            return true;
                            }





                            share|improve this answer












                            The solution about commitAllowingStateLoss works if your DialogFragment has no state to save, otherwise they will be lost like the function name told. But I think in most cases we have state to save, that is the major benefit of DialogFragment: Android recreate it and maintains its state automatically.



                            A better solution would be to check if the recreate process done, if not then return to caller, which is either an Activity or a FragmentActivity, it should call mark it and call the show function again later in its onPostResume() or onResumeFragments() callback, which we can make sure all fragments are recreated.



                            Here is an overridden show() from a subclass of DialogFragment:



                            public boolean show(FragmentManager fragmentManager) {
                            if (fragmentManager.isStateSaved()) return false;
                            show(fragmentManager, tagName);
                            return true;
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 10 '17 at 19:21









                            cn123h

                            1,4291412




                            1,4291412






















                                up vote
                                0
                                down vote













                                origin dialog fragment



                                public void show(FragmentManager manager, String tag) {
                                mDismissed = false;
                                mShownByMe = true;
                                FragmentTransaction ft = manager.beginTransaction();
                                ft.add(this, tag);
                                ft.commit(); //replace it by commitAllowingStateLoss
                                }



                                i don't know mDismissed, mShownByMe variables used for, so should be better if override show(FagmentManager, String) method of DialogFragment and it works fine with me



                                override fun show(manager: FragmentManager?, tag: String?) {
                                if (manager?.isDestroyed == false && !manager.isStateSaved) {
                                super.show(manager, tag)
                                }
                                }



                                isStateSaved available from appcompat >= 26.0.0 or androidx






                                share|improve this answer



























                                  up vote
                                  0
                                  down vote













                                  origin dialog fragment



                                  public void show(FragmentManager manager, String tag) {
                                  mDismissed = false;
                                  mShownByMe = true;
                                  FragmentTransaction ft = manager.beginTransaction();
                                  ft.add(this, tag);
                                  ft.commit(); //replace it by commitAllowingStateLoss
                                  }



                                  i don't know mDismissed, mShownByMe variables used for, so should be better if override show(FagmentManager, String) method of DialogFragment and it works fine with me



                                  override fun show(manager: FragmentManager?, tag: String?) {
                                  if (manager?.isDestroyed == false && !manager.isStateSaved) {
                                  super.show(manager, tag)
                                  }
                                  }



                                  isStateSaved available from appcompat >= 26.0.0 or androidx






                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    origin dialog fragment



                                    public void show(FragmentManager manager, String tag) {
                                    mDismissed = false;
                                    mShownByMe = true;
                                    FragmentTransaction ft = manager.beginTransaction();
                                    ft.add(this, tag);
                                    ft.commit(); //replace it by commitAllowingStateLoss
                                    }



                                    i don't know mDismissed, mShownByMe variables used for, so should be better if override show(FagmentManager, String) method of DialogFragment and it works fine with me



                                    override fun show(manager: FragmentManager?, tag: String?) {
                                    if (manager?.isDestroyed == false && !manager.isStateSaved) {
                                    super.show(manager, tag)
                                    }
                                    }



                                    isStateSaved available from appcompat >= 26.0.0 or androidx






                                    share|improve this answer














                                    origin dialog fragment



                                    public void show(FragmentManager manager, String tag) {
                                    mDismissed = false;
                                    mShownByMe = true;
                                    FragmentTransaction ft = manager.beginTransaction();
                                    ft.add(this, tag);
                                    ft.commit(); //replace it by commitAllowingStateLoss
                                    }



                                    i don't know mDismissed, mShownByMe variables used for, so should be better if override show(FagmentManager, String) method of DialogFragment and it works fine with me



                                    override fun show(manager: FragmentManager?, tag: String?) {
                                    if (manager?.isDestroyed == false && !manager.isStateSaved) {
                                    super.show(manager, tag)
                                    }
                                    }



                                    isStateSaved available from appcompat >= 26.0.0 or androidx







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 22 at 7:42

























                                    answered Nov 22 at 7:25









                                    vuhung3990

                                    4,13913037




                                    4,13913037






























                                        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.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • 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%2f30424319%2fcommitallowingstateloss-on-dialogfragment%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

                                        Berounka

                                        Sphinx de Gizeh

                                        Different font size/position of beamer's navigation symbols template's content depending on regular/plain...