Cerrar la ventana emergente de Android con la tecla de retroceso

He creado una aplicación de Android donde he creado una pantalla emergente. Pero cuando estoy presionando el botón de regreso, popup no se está cerrando.

He intentado con onBackPressed (). No está trabajando.

¿Puede alguien decirme qué hacer.

Saludos,

Shankar

Lo que debe hacer es llamar a setBackgroundDrawable en su PopupWindow después de inicializarlo. Algo como:

myPopup.setBackgroundDrawable(new BitmapDrawable()); 
 LayoutInflater layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); View popupView = layoutInflater.inflate(R.layout.popup_window_country_list, null); countryPopup = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); countryPopup.setBackgroundDrawable (new BitmapDrawable()); countryPopup.setFocusable(true); //Make Here True For back press dismiss countryPopup.setOutsideTouchable(true); countryPopup.setTouchInterceptor(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { countryPopup.dismiss(); return true; } return false; } }); 

Recientemente trabajé con ListPopupWindow ( android.support.v7.internal.widget.ListPopupWindow ) y el botón de vuelta comenzó a trabajar cuando llamé

 popupWindow.setModal(true); 

No importa lo que establezca en el método setBackgroundDrawable como suponen otras soluciones aquí.

 //here "popUp" is ref of PopupWindow popUp.setBackgroundDrawable(new BitmapDrawable());// it is most important peace of code // For Key Listeners View v = popUp.getContentView(); //Here assigning the key Listners v.setOnKeyListener(this); @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK) popUp.dismiss(); return false; }//Implements the KeyListener //and be careful we should implement "OnKeyListener"` 

Espero que sea útil (soy el nuevo usuario)

El siguiente código funciona perfectamente. Por lo tanto, anula la siguiente función en tu actividad

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //Changes 'back' button action if(keyCode==KeyEvent.KEYCODE_BACK) { if(!popUpHelper.isPopupShowing()){ onBackPressed(); }else{ popUpHelper.dismiss(); } } return super.onKeyDown(keyCode, event); } class PopupHelper { PopupWindow popupWindowAttachment; public void initAndShow(Activity context,int mToolbarHeight){ View layout = activity.getLayoutInflater().inflate( R.layout.activity_chat_attachment_popup, null); //create and show and Make sure popup window focusable should be false popupWindowAttachment = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindowAttachment.showAsDropDown(layout, 0, mToolbarHeight + (mToolbarHeight / 2) - 5); } public void dismiss() { if (isPopupShowing()) popupWindowAttachment.dismiss(); } public boolean isPopupShowing() { return popupWindowAttachment==null?false:popupWindowAttachment .isShowing(); } } 

100% pop-up se descartará en la parte posterior de prensa. Reemplace su código emergente con el siguiente código

 public void popup() { View popUpView_pur = getActivity().getLayoutInflater().inflate(R.layout.popup, null); PopupWindow popuplayout_pur = new PopupWindow(popUpView_pur, -1, -1, true); popuplayout_pur.setBackgroundDrawable(new BitmapDrawable()); popuplayout_pur.setOutsideTouchable(true); popuplayout_pur.showAtLocation(popUpView_pur, 17, 0, 0); } 

(o)

 public void popup() { // TODO Auto-generated method stub LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View popupView = inflater.inflate(R.layout.popuplayout, null, false); PopupWindow pw = new PopupWindow(getActivity()); pw.setWidth(WindowManager.LayoutParams.MATCH_PARENT); pw.setHeight(WindowManager.LayoutParams.MATCH_PARENT); pw.setTouchable(true); pw.setFocusable(true); pw.setOutsideTouchable(true); pw.setContentView(popupView); pw.showAtLocation(popupView, Gravity.CENTER, 0, 0); } 
 popup.setBackgroundDrawable(new BitmapDrawable()); popup.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { //do your code here } }); 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.