Dismiss non-cancelable dialog on fragment backPress - Android









up vote
6
down vote

favorite
1












I have a Navigation drawer activity and many fragments that I reach via the nav drawer.



In some of these fragments, I show a dialog which says "Loading.." while background tasks are happening.



Now I've made my dialogs not-cancellable by dialog.setCancelable(false) so that the user doesn't accidentally dismiss it by clicking anywhere on the screen. This makes it un-cancelable even when the phone back button is pressed.



This is the code for my dialog -



Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_progress_dialog);
((TextView)dialog.findViewById(R.id.custom_dialog_message)).setText("Loading ...");

dialog.setCancelable(false);
dialog.show();


I need to write code to dismiss the loading dialog and go to the previous fragment when the phone back button is pressed while on any fragment.



Can somebody help me out? Mostly I need implementing backPress specific to a fragment. Thanks!










share|improve this question

























    up vote
    6
    down vote

    favorite
    1












    I have a Navigation drawer activity and many fragments that I reach via the nav drawer.



    In some of these fragments, I show a dialog which says "Loading.." while background tasks are happening.



    Now I've made my dialogs not-cancellable by dialog.setCancelable(false) so that the user doesn't accidentally dismiss it by clicking anywhere on the screen. This makes it un-cancelable even when the phone back button is pressed.



    This is the code for my dialog -



    Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_progress_dialog);
    ((TextView)dialog.findViewById(R.id.custom_dialog_message)).setText("Loading ...");

    dialog.setCancelable(false);
    dialog.show();


    I need to write code to dismiss the loading dialog and go to the previous fragment when the phone back button is pressed while on any fragment.



    Can somebody help me out? Mostly I need implementing backPress specific to a fragment. Thanks!










    share|improve this question























      up vote
      6
      down vote

      favorite
      1









      up vote
      6
      down vote

      favorite
      1






      1





      I have a Navigation drawer activity and many fragments that I reach via the nav drawer.



      In some of these fragments, I show a dialog which says "Loading.." while background tasks are happening.



      Now I've made my dialogs not-cancellable by dialog.setCancelable(false) so that the user doesn't accidentally dismiss it by clicking anywhere on the screen. This makes it un-cancelable even when the phone back button is pressed.



      This is the code for my dialog -



      Dialog dialog = new Dialog(context);
      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
      dialog.setContentView(R.layout.custom_progress_dialog);
      ((TextView)dialog.findViewById(R.id.custom_dialog_message)).setText("Loading ...");

      dialog.setCancelable(false);
      dialog.show();


      I need to write code to dismiss the loading dialog and go to the previous fragment when the phone back button is pressed while on any fragment.



      Can somebody help me out? Mostly I need implementing backPress specific to a fragment. Thanks!










      share|improve this question













      I have a Navigation drawer activity and many fragments that I reach via the nav drawer.



      In some of these fragments, I show a dialog which says "Loading.." while background tasks are happening.



      Now I've made my dialogs not-cancellable by dialog.setCancelable(false) so that the user doesn't accidentally dismiss it by clicking anywhere on the screen. This makes it un-cancelable even when the phone back button is pressed.



      This is the code for my dialog -



      Dialog dialog = new Dialog(context);
      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
      dialog.setContentView(R.layout.custom_progress_dialog);
      ((TextView)dialog.findViewById(R.id.custom_dialog_message)).setText("Loading ...");

      dialog.setCancelable(false);
      dialog.show();


      I need to write code to dismiss the loading dialog and go to the previous fragment when the phone back button is pressed while on any fragment.



      Can somebody help me out? Mostly I need implementing backPress specific to a fragment. Thanks!







      android dialog onbackpressed






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 30 '15 at 10:17









      Mallika

      4151825




      4151825






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          you can use getFragmentManager().popBackStackImmediate();



          dialog.setOnKeyListener(new Dialog.OnKeyListener() 
          @Override
          public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
          // TODO Auto-generated method stub
          if (keyCode == KeyEvent.KEYCODE_BACK)
          dialog.dismiss();
          getFragmentManager().popBackStackImmediate();

          return true;

          );





          share|improve this answer




















          • Thanks for the addition to go back to the previous fragment. Worked well!
            – Mallika
            Sep 30 '15 at 10:40










          • my pleasure....
            – Usman Kurd
            Sep 30 '15 at 10:41

















          up vote
          7
          down vote













          It's simple as this



          dialog.setOnKeyListener(new Dialog.OnKeyListener() 
          @Override
          public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
          // TODO Auto-generated method stub
          if (keyCode == KeyEvent.KEYCODE_BACK)
          dialog.dismiss();

          return true;

          );





          share|improve this answer



























            up vote
            6
            down vote













            Remove your code line :



            dialog.setCancelable(false); 


            and put this one and try



            dialog.setCanceledOnTouchOutside(false);


            With this your dialog will not cancel if user accidentally touch on the screen, but if user press back button then it will cancel






            share|improve this answer



























              up vote
              0
              down vote













              please put the following code where you are initializing the alertDialogbuilder.



               alertDialogBuilder = new AlertDialog.Builder(this);

              alertDialogBuilder.setOnKeyListener(new Dialog.OnKeyListener()
              @Override
              public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
              // TODO Auto-generated method stub
              if (keyCode == KeyEvent.KEYCODE_BACK)
              dialog.dismiss();
              getFragmentManager().popBackStackImmediate();

              return true;

              );





              share|improve this answer










              New contributor




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

















                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',
                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%2f32863557%2fdismiss-non-cancelable-dialog-on-fragment-backpress-android%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                7
                down vote



                accepted










                you can use getFragmentManager().popBackStackImmediate();



                dialog.setOnKeyListener(new Dialog.OnKeyListener() 
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK)
                dialog.dismiss();
                getFragmentManager().popBackStackImmediate();

                return true;

                );





                share|improve this answer




















                • Thanks for the addition to go back to the previous fragment. Worked well!
                  – Mallika
                  Sep 30 '15 at 10:40










                • my pleasure....
                  – Usman Kurd
                  Sep 30 '15 at 10:41














                up vote
                7
                down vote



                accepted










                you can use getFragmentManager().popBackStackImmediate();



                dialog.setOnKeyListener(new Dialog.OnKeyListener() 
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK)
                dialog.dismiss();
                getFragmentManager().popBackStackImmediate();

                return true;

                );





                share|improve this answer




















                • Thanks for the addition to go back to the previous fragment. Worked well!
                  – Mallika
                  Sep 30 '15 at 10:40










                • my pleasure....
                  – Usman Kurd
                  Sep 30 '15 at 10:41












                up vote
                7
                down vote



                accepted







                up vote
                7
                down vote



                accepted






                you can use getFragmentManager().popBackStackImmediate();



                dialog.setOnKeyListener(new Dialog.OnKeyListener() 
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK)
                dialog.dismiss();
                getFragmentManager().popBackStackImmediate();

                return true;

                );





                share|improve this answer












                you can use getFragmentManager().popBackStackImmediate();



                dialog.setOnKeyListener(new Dialog.OnKeyListener() 
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK)
                dialog.dismiss();
                getFragmentManager().popBackStackImmediate();

                return true;

                );






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 30 '15 at 10:28









                Usman Kurd

                6,49664376




                6,49664376











                • Thanks for the addition to go back to the previous fragment. Worked well!
                  – Mallika
                  Sep 30 '15 at 10:40










                • my pleasure....
                  – Usman Kurd
                  Sep 30 '15 at 10:41
















                • Thanks for the addition to go back to the previous fragment. Worked well!
                  – Mallika
                  Sep 30 '15 at 10:40










                • my pleasure....
                  – Usman Kurd
                  Sep 30 '15 at 10:41















                Thanks for the addition to go back to the previous fragment. Worked well!
                – Mallika
                Sep 30 '15 at 10:40




                Thanks for the addition to go back to the previous fragment. Worked well!
                – Mallika
                Sep 30 '15 at 10:40












                my pleasure....
                – Usman Kurd
                Sep 30 '15 at 10:41




                my pleasure....
                – Usman Kurd
                Sep 30 '15 at 10:41












                up vote
                7
                down vote













                It's simple as this



                dialog.setOnKeyListener(new Dialog.OnKeyListener() 
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK)
                dialog.dismiss();

                return true;

                );





                share|improve this answer
























                  up vote
                  7
                  down vote













                  It's simple as this



                  dialog.setOnKeyListener(new Dialog.OnKeyListener() 
                  @Override
                  public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                  // TODO Auto-generated method stub
                  if (keyCode == KeyEvent.KEYCODE_BACK)
                  dialog.dismiss();

                  return true;

                  );





                  share|improve this answer






















                    up vote
                    7
                    down vote










                    up vote
                    7
                    down vote









                    It's simple as this



                    dialog.setOnKeyListener(new Dialog.OnKeyListener() 
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK)
                    dialog.dismiss();

                    return true;

                    );





                    share|improve this answer












                    It's simple as this



                    dialog.setOnKeyListener(new Dialog.OnKeyListener() 
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK)
                    dialog.dismiss();

                    return true;

                    );






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 30 '15 at 10:19









                    NaviRamyle

                    3,19212247




                    3,19212247




















                        up vote
                        6
                        down vote













                        Remove your code line :



                        dialog.setCancelable(false); 


                        and put this one and try



                        dialog.setCanceledOnTouchOutside(false);


                        With this your dialog will not cancel if user accidentally touch on the screen, but if user press back button then it will cancel






                        share|improve this answer
























                          up vote
                          6
                          down vote













                          Remove your code line :



                          dialog.setCancelable(false); 


                          and put this one and try



                          dialog.setCanceledOnTouchOutside(false);


                          With this your dialog will not cancel if user accidentally touch on the screen, but if user press back button then it will cancel






                          share|improve this answer






















                            up vote
                            6
                            down vote










                            up vote
                            6
                            down vote









                            Remove your code line :



                            dialog.setCancelable(false); 


                            and put this one and try



                            dialog.setCanceledOnTouchOutside(false);


                            With this your dialog will not cancel if user accidentally touch on the screen, but if user press back button then it will cancel






                            share|improve this answer












                            Remove your code line :



                            dialog.setCancelable(false); 


                            and put this one and try



                            dialog.setCanceledOnTouchOutside(false);


                            With this your dialog will not cancel if user accidentally touch on the screen, but if user press back button then it will cancel







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 30 '15 at 10:24









                            warlock

                            1,5301223




                            1,5301223




















                                up vote
                                0
                                down vote













                                please put the following code where you are initializing the alertDialogbuilder.



                                 alertDialogBuilder = new AlertDialog.Builder(this);

                                alertDialogBuilder.setOnKeyListener(new Dialog.OnKeyListener()
                                @Override
                                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                                // TODO Auto-generated method stub
                                if (keyCode == KeyEvent.KEYCODE_BACK)
                                dialog.dismiss();
                                getFragmentManager().popBackStackImmediate();

                                return true;

                                );





                                share|improve this answer










                                New contributor




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





















                                  up vote
                                  0
                                  down vote













                                  please put the following code where you are initializing the alertDialogbuilder.



                                   alertDialogBuilder = new AlertDialog.Builder(this);

                                  alertDialogBuilder.setOnKeyListener(new Dialog.OnKeyListener()
                                  @Override
                                  public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                                  // TODO Auto-generated method stub
                                  if (keyCode == KeyEvent.KEYCODE_BACK)
                                  dialog.dismiss();
                                  getFragmentManager().popBackStackImmediate();

                                  return true;

                                  );





                                  share|improve this answer










                                  New contributor




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



















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    please put the following code where you are initializing the alertDialogbuilder.



                                     alertDialogBuilder = new AlertDialog.Builder(this);

                                    alertDialogBuilder.setOnKeyListener(new Dialog.OnKeyListener()
                                    @Override
                                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                                    // TODO Auto-generated method stub
                                    if (keyCode == KeyEvent.KEYCODE_BACK)
                                    dialog.dismiss();
                                    getFragmentManager().popBackStackImmediate();

                                    return true;

                                    );





                                    share|improve this answer










                                    New contributor




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









                                    please put the following code where you are initializing the alertDialogbuilder.



                                     alertDialogBuilder = new AlertDialog.Builder(this);

                                    alertDialogBuilder.setOnKeyListener(new Dialog.OnKeyListener()
                                    @Override
                                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
                                    // TODO Auto-generated method stub
                                    if (keyCode == KeyEvent.KEYCODE_BACK)
                                    dialog.dismiss();
                                    getFragmentManager().popBackStackImmediate();

                                    return true;

                                    );






                                    share|improve this answer










                                    New contributor




                                    francis 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 answer



                                    share|improve this answer








                                    edited Nov 10 at 15:35









                                    Michal

                                    827919




                                    827919






                                    New contributor




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









                                    answered Nov 10 at 10:29









                                    francis

                                    1




                                    1




                                    New contributor




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





                                    New contributor





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






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



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32863557%2fdismiss-non-cancelable-dialog-on-fragment-backpress-android%23new-answer', 'question_page');

                                        );

                                        Post as a guest














































































                                        Popular posts from this blog

                                        Top Tejano songwriter Luis Silva dead of heart attack at 64

                                        政党

                                        天津地下鉄3号線