Descartar la ventana emergente por el botón Atrás

Quiero descartar la ventana emergente haciendo clic fuera de la ventana emergente o por el botón de volver, pero cuando haga clic en el botón de nuevo mi aplicación de salida, en lugar de salir de la aplicación quiero cerrar la ventana emergente.

Aquí está mi código,

ivmainmenu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub LayoutInflater layoutInflater = (LayoutInflater)getBaseContext() .getSystemService(LAYOUT_INFLATER_SERVICE); View popupView = layoutInflater.inflate(R.layout.popupwindow, null); final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); popupWindow.showAsDropDown(ivmainmenu, 0,14); popupView.setPadding(0, 0, 0, 10); popupWindow.showAsDropDown(ivmainmenu); popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setOutsideTouchable(false); TextView tvpopupwork = (TextView)popupView.findViewById(R.id.tvpopupwork); TextView tvpopupabout = (TextView)popupView.findViewById(R.id.tvpopupabout); TextView tvpopupservices = (TextView)popupView.findViewById(R.id.tvpopupservices); TextView tvpopupcontact = (TextView)popupView.findViewById(R.id.tvpopupcontact); Typeface typeFace2 = Typeface.createFromAsset(getAssets(),"fonts/arboriaboldregular.ttf"); tvpopupwork.setTypeface(typeFace2); tvpopupabout.setTypeface(typeFace2); tvpopupservices.setTypeface(typeFace2); tvpopupcontact.setTypeface(typeFace2); tvpopupwork.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(Home.this,Ourwork.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); } }); tvpopupabout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(Home.this,Aboutus.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); } }); tvpopupservices.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(Home.this,Services.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); } }); tvpopupcontact.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(Home.this,Contact.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); } }); ivmainmenu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub popupWindow.dismiss(); } } }); 

Su me da el resultado lo que quiero, pero cuando cierra el menú de la no se abre de nuevo, quiero abrir de nuevo, ¿qué debo hacer? gracias.

Reemplazar

 popupWindow.setOutsideTouchable(false); 

con este

 popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); 

Mantener referencia global para PopUpWindow y anular onBackPressed()

 @Override public void onBackPressed() { if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } else { super.onBackPressed(); } } 

Para descartar por el mismo Button

  ivmainmenu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; } else { // show pop up now } } }); 

Por favor escriba onBackPressed( ) y tenga el siguiente código

 if(popup!=null){ //dismiss the popup popup.dismiss(); //make popup null again popup=null; } 
 popupWindow.setBackgroundDrawable (new BitmapDrawable()); popupWindow.setOutsideTouchable(true); ;) 

Prueba esto..

Utilizar PopupWindow popupWindow como variable global

popup.setOutsideTouchable(true);

 @Override public void onBackPressed() { if (popupWindow != null) { if (popupWindow.isShowing()) { popupWindow.dismiss(); } } else { finish(); } } 

Pruebe de esta manera: Implementar onBackPressed() y agregar

 if(popup!=null) { popup.dismiss(); popup=null; } 

Y establezca su PopWindow con a continuación:

 popup.setOutsideTouchable(true); 

Puede anular la devolución de llamada onBackPressed () en su código y comprobar para ver si su ventana emergente ya está mostrando (luego lo despide), de lo contrario usted llama super para obtener un comportamiento normal.

  • Desenfoque o fondo oscuro cuando Android PopupWindow está activo
  • Android - utilizar spinner omisión sombra predeterminada
  • ¿Cómo mostrar correctamente un menú emergente debajo de un TextView, similar a Spinner?
  • Error en Spinner en una ventana emergente, se bloquea cuando se hace clic
  • EditText setError no funciona en PopupWindow
  • ¿Hay un ejemplo simple de la clase PopupWindow que utiliza Android v2.0?
  • ¿Cómo puedo reemplazar mi código popupmenu con la lista popupwindow?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.