Add action on fragment before exit the App










0














I have a small problem of coding.



My goal: I have 2 fragment [0] and [1]. My default fragment is [0]. Once on [1] I press the return key and I return to the fragment [0] (instead of closing the application).



Here is a code that works correctly,



knowing that the fragment [1] is already on the stack:



getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FragmentUn().addToBackStack(null).commit();


and here is the code on the backPressed:



@Override
public void onBackPressed()
if (drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);

else if (getFragmentManager().getBackStackEntryCount() > 0)
getFragmentManager().popBackStack();
else
super.onBackPressed();





This code works very well but what I want to do is to add an action (popUp) once to arrive on the fragment [0] which asks the user if he wants to leave the application or not when one presses the return key (here the app simply closes)



To do this, I wrote:



@Override
public void onBackPressed()
if (drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);

else if (getFragmentManager().getBackStackEntryCount() > 0)
getFragmentManager().popBackStack();
else
AlertDialog.Builder exitPopUp = new AlertDialog.Builder(this);
exitPopUp.setTitle("Exit");
exitPopUp.setMessage("Voulez-vous quitter L'App ?");
exitPopUp.setPositiveButton("Oui", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialogInterface, int i)
finish();

);
exitPopUp.setNegativeButton("Non", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialogInterface, int i)


);
exitPopUp.setNeutralButton("Noter", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialogInterface, int i)
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
try
startActivity(goToMarket);
catch (ActivityNotFoundException e)
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName())));


);

exitPopUp.show();




Result ==> The app immediately displays the popUp when you press return on either fragment [0] or [1]. Except what I wish is only to arrive on fragment [0] that popUp is displayed. [1] - return key -> [0] - return key -> Exit popUp.



So what must I do to make it work properly? Thank you in advance :)










share|improve this question




























    0














    I have a small problem of coding.



    My goal: I have 2 fragment [0] and [1]. My default fragment is [0]. Once on [1] I press the return key and I return to the fragment [0] (instead of closing the application).



    Here is a code that works correctly,



    knowing that the fragment [1] is already on the stack:



    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FragmentUn().addToBackStack(null).commit();


    and here is the code on the backPressed:



    @Override
    public void onBackPressed()
    if (drawer.isDrawerOpen(GravityCompat.START))
    drawer.closeDrawer(GravityCompat.START);

    else if (getFragmentManager().getBackStackEntryCount() > 0)
    getFragmentManager().popBackStack();
    else
    super.onBackPressed();





    This code works very well but what I want to do is to add an action (popUp) once to arrive on the fragment [0] which asks the user if he wants to leave the application or not when one presses the return key (here the app simply closes)



    To do this, I wrote:



    @Override
    public void onBackPressed()
    if (drawer.isDrawerOpen(GravityCompat.START))
    drawer.closeDrawer(GravityCompat.START);

    else if (getFragmentManager().getBackStackEntryCount() > 0)
    getFragmentManager().popBackStack();
    else
    AlertDialog.Builder exitPopUp = new AlertDialog.Builder(this);
    exitPopUp.setTitle("Exit");
    exitPopUp.setMessage("Voulez-vous quitter L'App ?");
    exitPopUp.setPositiveButton("Oui", new DialogInterface.OnClickListener()
    @Override
    public void onClick(DialogInterface dialogInterface, int i)
    finish();

    );
    exitPopUp.setNegativeButton("Non", new DialogInterface.OnClickListener()
    @Override
    public void onClick(DialogInterface dialogInterface, int i)


    );
    exitPopUp.setNeutralButton("Noter", new DialogInterface.OnClickListener()
    @Override
    public void onClick(DialogInterface dialogInterface, int i)
    Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    try
    startActivity(goToMarket);
    catch (ActivityNotFoundException e)
    startActivity(new Intent(Intent.ACTION_VIEW,
    Uri.parse("http://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName())));


    );

    exitPopUp.show();




    Result ==> The app immediately displays the popUp when you press return on either fragment [0] or [1]. Except what I wish is only to arrive on fragment [0] that popUp is displayed. [1] - return key -> [0] - return key -> Exit popUp.



    So what must I do to make it work properly? Thank you in advance :)










    share|improve this question


























      0












      0








      0


      1





      I have a small problem of coding.



      My goal: I have 2 fragment [0] and [1]. My default fragment is [0]. Once on [1] I press the return key and I return to the fragment [0] (instead of closing the application).



      Here is a code that works correctly,



      knowing that the fragment [1] is already on the stack:



      getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FragmentUn().addToBackStack(null).commit();


      and here is the code on the backPressed:



      @Override
      public void onBackPressed()
      if (drawer.isDrawerOpen(GravityCompat.START))
      drawer.closeDrawer(GravityCompat.START);

      else if (getFragmentManager().getBackStackEntryCount() > 0)
      getFragmentManager().popBackStack();
      else
      super.onBackPressed();





      This code works very well but what I want to do is to add an action (popUp) once to arrive on the fragment [0] which asks the user if he wants to leave the application or not when one presses the return key (here the app simply closes)



      To do this, I wrote:



      @Override
      public void onBackPressed()
      if (drawer.isDrawerOpen(GravityCompat.START))
      drawer.closeDrawer(GravityCompat.START);

      else if (getFragmentManager().getBackStackEntryCount() > 0)
      getFragmentManager().popBackStack();
      else
      AlertDialog.Builder exitPopUp = new AlertDialog.Builder(this);
      exitPopUp.setTitle("Exit");
      exitPopUp.setMessage("Voulez-vous quitter L'App ?");
      exitPopUp.setPositiveButton("Oui", new DialogInterface.OnClickListener()
      @Override
      public void onClick(DialogInterface dialogInterface, int i)
      finish();

      );
      exitPopUp.setNegativeButton("Non", new DialogInterface.OnClickListener()
      @Override
      public void onClick(DialogInterface dialogInterface, int i)


      );
      exitPopUp.setNeutralButton("Noter", new DialogInterface.OnClickListener()
      @Override
      public void onClick(DialogInterface dialogInterface, int i)
      Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
      try
      startActivity(goToMarket);
      catch (ActivityNotFoundException e)
      startActivity(new Intent(Intent.ACTION_VIEW,
      Uri.parse("http://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName())));


      );

      exitPopUp.show();




      Result ==> The app immediately displays the popUp when you press return on either fragment [0] or [1]. Except what I wish is only to arrive on fragment [0] that popUp is displayed. [1] - return key -> [0] - return key -> Exit popUp.



      So what must I do to make it work properly? Thank you in advance :)










      share|improve this question















      I have a small problem of coding.



      My goal: I have 2 fragment [0] and [1]. My default fragment is [0]. Once on [1] I press the return key and I return to the fragment [0] (instead of closing the application).



      Here is a code that works correctly,



      knowing that the fragment [1] is already on the stack:



      getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FragmentUn().addToBackStack(null).commit();


      and here is the code on the backPressed:



      @Override
      public void onBackPressed()
      if (drawer.isDrawerOpen(GravityCompat.START))
      drawer.closeDrawer(GravityCompat.START);

      else if (getFragmentManager().getBackStackEntryCount() > 0)
      getFragmentManager().popBackStack();
      else
      super.onBackPressed();





      This code works very well but what I want to do is to add an action (popUp) once to arrive on the fragment [0] which asks the user if he wants to leave the application or not when one presses the return key (here the app simply closes)



      To do this, I wrote:



      @Override
      public void onBackPressed()
      if (drawer.isDrawerOpen(GravityCompat.START))
      drawer.closeDrawer(GravityCompat.START);

      else if (getFragmentManager().getBackStackEntryCount() > 0)
      getFragmentManager().popBackStack();
      else
      AlertDialog.Builder exitPopUp = new AlertDialog.Builder(this);
      exitPopUp.setTitle("Exit");
      exitPopUp.setMessage("Voulez-vous quitter L'App ?");
      exitPopUp.setPositiveButton("Oui", new DialogInterface.OnClickListener()
      @Override
      public void onClick(DialogInterface dialogInterface, int i)
      finish();

      );
      exitPopUp.setNegativeButton("Non", new DialogInterface.OnClickListener()
      @Override
      public void onClick(DialogInterface dialogInterface, int i)


      );
      exitPopUp.setNeutralButton("Noter", new DialogInterface.OnClickListener()
      @Override
      public void onClick(DialogInterface dialogInterface, int i)
      Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
      try
      startActivity(goToMarket);
      catch (ActivityNotFoundException e)
      startActivity(new Intent(Intent.ACTION_VIEW,
      Uri.parse("http://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName())));


      );

      exitPopUp.show();




      Result ==> The app immediately displays the popUp when you press return on either fragment [0] or [1]. Except what I wish is only to arrive on fragment [0] that popUp is displayed. [1] - return key -> [0] - return key -> Exit popUp.



      So what must I do to make it work properly? Thank you in advance :)







      java android android-fragments fragment-backstack






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 5:54

























      asked Nov 12 at 13:40









      Antonely93

      12




      12






















          2 Answers
          2






          active

          oldest

          votes


















          0














           @Override
          public void onBackPressed()
          super.onBackPressed();
          MainFragment main = (MainFragment) getSupportFragmentManager().findFragmentByTag("main");
          if (main != null && main.isVisible())
          if (mExitCheck < 1)
          Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
          else
          //mExitCheck = 0 is an int
          mExitCheck = 0;

          //Finish() to exit the app
          finish();

          mExitCheck++;
          else
          showMain();







          share|improve this answer




















          • thank you for the answer. Sorry but I am a little lost in your code. I can do anything on mExitCheck i have the error: cannot resolve symbol and on showMain(): cannot resolve method. Can you please inject (edit) it on mine, if i want to show the popUp on the fragment [0] ?
            – Antonely93
            Nov 13 at 5:44










          • Check out the new answer
            – Lego
            Nov 15 at 13:26


















          0














          easiest thing to do is set an int to 0(OR any flag that lets you know you are on the first fragment) something like this.



           //do this for your first fragment
          Fragment fragment = MainFragment.newInstance();
          FragmentTransaction transaction =
          getSupportFragmentManager().beginTransaction();
          transaction.replace(R.id.layout_container, fragment, "main").commit();
          mCurrentFrag = 0;

          // and this for others
          Fragment fragment = MainFragment.newInstance();
          FragmentTransaction transaction =
          getSupportFragmentManager().beginTransaction();
          transaction.replace(R.id.layout_container, fragment, "main").commit();
          mCurrentFrag = 1;


          then do the following



          @Override
          public void onBackPressed()
          if (mCurrentFrag == MAIN_FRAG)
          runOnUiThread(new Runnable()
          @Override
          public void run()
          AlertDialog.Builder exitPopUp = new AlertDialog.Builder(MainActivity.this);
          exitPopUp.setTitle("Exit");
          exitPopUp.setMessage("Are you sure you want to exit?");
          exitPopUp.setPositiveButton("Oui", new DialogInterface.OnClickListener()
          @Override
          public void onClick(DialogInterface dialogInterface, int i)
          finish();

          );
          exitPopUp.setNegativeButton("Non", new DialogInterface.OnClickListener()
          @Override
          public void onClick(DialogInterface dialogInterface, int i)


          );
          exitPopUp.setNeutralButton("Noter", new DialogInterface.OnClickListener()
          @Override
          public void onClick(DialogInterface dialogInterface, int i)
          Intent.FLAG_ACTIVITY_NEW_DOCUMENT
          );

          exitPopUp.show();

          );
          else
          super.onBackPressed();







          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%2f53263427%2fadd-action-on-fragment-before-exit-the-app%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














             @Override
            public void onBackPressed()
            super.onBackPressed();
            MainFragment main = (MainFragment) getSupportFragmentManager().findFragmentByTag("main");
            if (main != null && main.isVisible())
            if (mExitCheck < 1)
            Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
            else
            //mExitCheck = 0 is an int
            mExitCheck = 0;

            //Finish() to exit the app
            finish();

            mExitCheck++;
            else
            showMain();







            share|improve this answer




















            • thank you for the answer. Sorry but I am a little lost in your code. I can do anything on mExitCheck i have the error: cannot resolve symbol and on showMain(): cannot resolve method. Can you please inject (edit) it on mine, if i want to show the popUp on the fragment [0] ?
              – Antonely93
              Nov 13 at 5:44










            • Check out the new answer
              – Lego
              Nov 15 at 13:26















            0














             @Override
            public void onBackPressed()
            super.onBackPressed();
            MainFragment main = (MainFragment) getSupportFragmentManager().findFragmentByTag("main");
            if (main != null && main.isVisible())
            if (mExitCheck < 1)
            Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
            else
            //mExitCheck = 0 is an int
            mExitCheck = 0;

            //Finish() to exit the app
            finish();

            mExitCheck++;
            else
            showMain();







            share|improve this answer




















            • thank you for the answer. Sorry but I am a little lost in your code. I can do anything on mExitCheck i have the error: cannot resolve symbol and on showMain(): cannot resolve method. Can you please inject (edit) it on mine, if i want to show the popUp on the fragment [0] ?
              – Antonely93
              Nov 13 at 5:44










            • Check out the new answer
              – Lego
              Nov 15 at 13:26













            0












            0








            0






             @Override
            public void onBackPressed()
            super.onBackPressed();
            MainFragment main = (MainFragment) getSupportFragmentManager().findFragmentByTag("main");
            if (main != null && main.isVisible())
            if (mExitCheck < 1)
            Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
            else
            //mExitCheck = 0 is an int
            mExitCheck = 0;

            //Finish() to exit the app
            finish();

            mExitCheck++;
            else
            showMain();







            share|improve this answer












             @Override
            public void onBackPressed()
            super.onBackPressed();
            MainFragment main = (MainFragment) getSupportFragmentManager().findFragmentByTag("main");
            if (main != null && main.isVisible())
            if (mExitCheck < 1)
            Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();
            else
            //mExitCheck = 0 is an int
            mExitCheck = 0;

            //Finish() to exit the app
            finish();

            mExitCheck++;
            else
            showMain();








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 13:48









            Lego

            409




            409











            • thank you for the answer. Sorry but I am a little lost in your code. I can do anything on mExitCheck i have the error: cannot resolve symbol and on showMain(): cannot resolve method. Can you please inject (edit) it on mine, if i want to show the popUp on the fragment [0] ?
              – Antonely93
              Nov 13 at 5:44










            • Check out the new answer
              – Lego
              Nov 15 at 13:26
















            • thank you for the answer. Sorry but I am a little lost in your code. I can do anything on mExitCheck i have the error: cannot resolve symbol and on showMain(): cannot resolve method. Can you please inject (edit) it on mine, if i want to show the popUp on the fragment [0] ?
              – Antonely93
              Nov 13 at 5:44










            • Check out the new answer
              – Lego
              Nov 15 at 13:26















            thank you for the answer. Sorry but I am a little lost in your code. I can do anything on mExitCheck i have the error: cannot resolve symbol and on showMain(): cannot resolve method. Can you please inject (edit) it on mine, if i want to show the popUp on the fragment [0] ?
            – Antonely93
            Nov 13 at 5:44




            thank you for the answer. Sorry but I am a little lost in your code. I can do anything on mExitCheck i have the error: cannot resolve symbol and on showMain(): cannot resolve method. Can you please inject (edit) it on mine, if i want to show the popUp on the fragment [0] ?
            – Antonely93
            Nov 13 at 5:44












            Check out the new answer
            – Lego
            Nov 15 at 13:26




            Check out the new answer
            – Lego
            Nov 15 at 13:26













            0














            easiest thing to do is set an int to 0(OR any flag that lets you know you are on the first fragment) something like this.



             //do this for your first fragment
            Fragment fragment = MainFragment.newInstance();
            FragmentTransaction transaction =
            getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.layout_container, fragment, "main").commit();
            mCurrentFrag = 0;

            // and this for others
            Fragment fragment = MainFragment.newInstance();
            FragmentTransaction transaction =
            getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.layout_container, fragment, "main").commit();
            mCurrentFrag = 1;


            then do the following



            @Override
            public void onBackPressed()
            if (mCurrentFrag == MAIN_FRAG)
            runOnUiThread(new Runnable()
            @Override
            public void run()
            AlertDialog.Builder exitPopUp = new AlertDialog.Builder(MainActivity.this);
            exitPopUp.setTitle("Exit");
            exitPopUp.setMessage("Are you sure you want to exit?");
            exitPopUp.setPositiveButton("Oui", new DialogInterface.OnClickListener()
            @Override
            public void onClick(DialogInterface dialogInterface, int i)
            finish();

            );
            exitPopUp.setNegativeButton("Non", new DialogInterface.OnClickListener()
            @Override
            public void onClick(DialogInterface dialogInterface, int i)


            );
            exitPopUp.setNeutralButton("Noter", new DialogInterface.OnClickListener()
            @Override
            public void onClick(DialogInterface dialogInterface, int i)
            Intent.FLAG_ACTIVITY_NEW_DOCUMENT
            );

            exitPopUp.show();

            );
            else
            super.onBackPressed();







            share|improve this answer

























              0














              easiest thing to do is set an int to 0(OR any flag that lets you know you are on the first fragment) something like this.



               //do this for your first fragment
              Fragment fragment = MainFragment.newInstance();
              FragmentTransaction transaction =
              getSupportFragmentManager().beginTransaction();
              transaction.replace(R.id.layout_container, fragment, "main").commit();
              mCurrentFrag = 0;

              // and this for others
              Fragment fragment = MainFragment.newInstance();
              FragmentTransaction transaction =
              getSupportFragmentManager().beginTransaction();
              transaction.replace(R.id.layout_container, fragment, "main").commit();
              mCurrentFrag = 1;


              then do the following



              @Override
              public void onBackPressed()
              if (mCurrentFrag == MAIN_FRAG)
              runOnUiThread(new Runnable()
              @Override
              public void run()
              AlertDialog.Builder exitPopUp = new AlertDialog.Builder(MainActivity.this);
              exitPopUp.setTitle("Exit");
              exitPopUp.setMessage("Are you sure you want to exit?");
              exitPopUp.setPositiveButton("Oui", new DialogInterface.OnClickListener()
              @Override
              public void onClick(DialogInterface dialogInterface, int i)
              finish();

              );
              exitPopUp.setNegativeButton("Non", new DialogInterface.OnClickListener()
              @Override
              public void onClick(DialogInterface dialogInterface, int i)


              );
              exitPopUp.setNeutralButton("Noter", new DialogInterface.OnClickListener()
              @Override
              public void onClick(DialogInterface dialogInterface, int i)
              Intent.FLAG_ACTIVITY_NEW_DOCUMENT
              );

              exitPopUp.show();

              );
              else
              super.onBackPressed();







              share|improve this answer























                0












                0








                0






                easiest thing to do is set an int to 0(OR any flag that lets you know you are on the first fragment) something like this.



                 //do this for your first fragment
                Fragment fragment = MainFragment.newInstance();
                FragmentTransaction transaction =
                getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.layout_container, fragment, "main").commit();
                mCurrentFrag = 0;

                // and this for others
                Fragment fragment = MainFragment.newInstance();
                FragmentTransaction transaction =
                getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.layout_container, fragment, "main").commit();
                mCurrentFrag = 1;


                then do the following



                @Override
                public void onBackPressed()
                if (mCurrentFrag == MAIN_FRAG)
                runOnUiThread(new Runnable()
                @Override
                public void run()
                AlertDialog.Builder exitPopUp = new AlertDialog.Builder(MainActivity.this);
                exitPopUp.setTitle("Exit");
                exitPopUp.setMessage("Are you sure you want to exit?");
                exitPopUp.setPositiveButton("Oui", new DialogInterface.OnClickListener()
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                finish();

                );
                exitPopUp.setNegativeButton("Non", new DialogInterface.OnClickListener()
                @Override
                public void onClick(DialogInterface dialogInterface, int i)


                );
                exitPopUp.setNeutralButton("Noter", new DialogInterface.OnClickListener()
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                Intent.FLAG_ACTIVITY_NEW_DOCUMENT
                );

                exitPopUp.show();

                );
                else
                super.onBackPressed();







                share|improve this answer












                easiest thing to do is set an int to 0(OR any flag that lets you know you are on the first fragment) something like this.



                 //do this for your first fragment
                Fragment fragment = MainFragment.newInstance();
                FragmentTransaction transaction =
                getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.layout_container, fragment, "main").commit();
                mCurrentFrag = 0;

                // and this for others
                Fragment fragment = MainFragment.newInstance();
                FragmentTransaction transaction =
                getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.layout_container, fragment, "main").commit();
                mCurrentFrag = 1;


                then do the following



                @Override
                public void onBackPressed()
                if (mCurrentFrag == MAIN_FRAG)
                runOnUiThread(new Runnable()
                @Override
                public void run()
                AlertDialog.Builder exitPopUp = new AlertDialog.Builder(MainActivity.this);
                exitPopUp.setTitle("Exit");
                exitPopUp.setMessage("Are you sure you want to exit?");
                exitPopUp.setPositiveButton("Oui", new DialogInterface.OnClickListener()
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                finish();

                );
                exitPopUp.setNegativeButton("Non", new DialogInterface.OnClickListener()
                @Override
                public void onClick(DialogInterface dialogInterface, int i)


                );
                exitPopUp.setNeutralButton("Noter", new DialogInterface.OnClickListener()
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                Intent.FLAG_ACTIVITY_NEW_DOCUMENT
                );

                exitPopUp.show();

                );
                else
                super.onBackPressed();








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 at 6:40









                Lego

                409




                409



























                    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%2f53263427%2fadd-action-on-fragment-before-exit-the-app%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

                    27

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    Category:Rhetoric