Naresh Kaushik
Naresh Kaushik

Reputation: 302

Handling Back key while showing a dialog in android

in my application i prompt for password in onResume(), and Before this i have already created view in onCreate() so it is compulsory for user to enter password But if user press the Back key password dialog get disappered and user easily use the application

when dialog is Being shown,if get key event for Back key then i can easily handle this for that purpose i implemented onBackPressed()/onKeyDown() methods of Activity But none get event for this key when dialog is on screen after dialog disappered these methods get event for Back key can anyone tell me how can i handle this case

thanks in advance

Upvotes: 2

Views: 4959

Answers (3)

dialog.setcancellable(false);

it works perfect

Upvotes: 1

Anand Sainath
Anand Sainath

Reputation: 1807

You can do something like this :

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
   if (keyCode == KeyEvent.KEYCODE_BACK && progressDialog.isShowing())
   {
       // DO SOMETHING
   }

   // Call super code so we dont limit default interaction
   super.onKeyDown(keyCode, event);

   return true;
}

Upvotes: -1

CommonsWare
CommonsWare

Reputation: 1006664

Call setCancelable() on your Dialog or your AlertDialog.Builder, depending on how you are creating this dialog.

Upvotes: 8

Related Questions