-2

In one of my applications I am using fragment. Now I want to add exit alertbox on back key press but I am not able to get public void onBackPressed() method in Fragment extended class. How can I achieve this? My MainActivity extends FragmentActivity.

public void onBackPressed() {   
    super.onBackPressed();
    alertbox("myappname", "Do You Want to Exit?");
}

But it does not display a dialog or if it displays it disappears in 1 second. What am I doing wrong.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
Android
  • 8,995
  • 9
  • 67
  • 108
  • http://stackoverflow.com/questions/5448653/how-to-implement-onbackpressed-in-android-fragments Possible duplicate of mentioned link – Gaurav Gupta Apr 22 '14 at 05:08

2 Answers2

0

use this to exit

code

 @Override
       public boolean onKeyDown(int keyCode, KeyEvent event) {
           if ((keyCode == KeyEvent.KEYCODE_BACK)) {
               AlertDialog.Builder alertbox = new AlertDialog.Builder(MainActivity.this);

               alertbox.setTitle("Do You Want To Exit ?");
               alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface arg0, int arg1) { 
                      // finish used for destroyed activity
                       exit();
                   }
               });

               alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface arg0, int arg1) {
                           // Nothing will be happened when clicked on no button 
                           // of Dialog     
                 }
               });

               alertbox.show();
           }
           return super.onKeyDown(keyCode, event);
       }
    public void exit()
    {
         Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_HOME);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
    }
Tushar Narang
  • 1,997
  • 3
  • 21
  • 49
0

this function works the same.... as on backpressed.... put it in your activity which is containing the fragment and if you have more than one fragment then check for the fragment from which you want to show your exit alert box........

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // Handle the back button
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            // Ask the user if they want to quit
            if (yourfragment.isVisible()) {
                new AlertDialog.Builder(this)

                        .setIcon(android.R.drawable.ic_dialog_alert)

                        .setTitle("Exit")

                        .setMessage("Are you sure you want to leave?")

                        .setNegativeButton(android.R.string.cancel, null)

                        .setPositiveButton(android.R.string.ok,
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {

                                        // Exit the activity

                                        finish();

                                    }

                                })

                        .show();

                // Say that we've consumed the event
                return true;

            }

        }

        return super.onKeyDown(keyCode, event);

    }
Tushar Narang
  • 1,997
  • 3
  • 21
  • 49
Aritra
  • 436
  • 2
  • 6