0

I have Fragment A which contains two button (Let say ButtonA and ButtonB), while pressing the ButtonB i am loading the fragment B (fragment B contains 1 Button, Let say Button C),while pressing button C ,the pop window is going to appear.

The problem I am facing now , when i back press then popwindow is disapperaing but loading Fragment A.

Actually I need when popwindow is showing in fragment B ,and user press back button then ,pop window should only disappear and it remain still in Fragment B ,not go to fragment A.

what i did , from the fragment B i had created one interface and pass the Popwindow instance in an Activity , and from the activity i am dismissing the popwindow.

 @Override
    public void onHandleRequest(PopupWindow popupWindow, PopupWindow backpopwindow, String fragmentName) {
        if(fragmentName !=null){
            this.fragmentName=fragmentName;
          if(popupWindow!=null){
              this.popupWindow=popupWindow;
              this.backpopwindow=backpopwindow;

          }
        }



@Override
    public void onBackPressed() {
        super.onBackPressed();

        if(popupWindow!=null && popupWindow.isShowing()){
            popupWindow.dismiss();
            backpopwindow.dismiss();
        }
}

In My Fragment Class :

public interface OnHandleCallBack{
        public void onHandleRequest(PopupWindow popupWindow,PopupWindow backpopwindow,String fragmentName);
    }

public void showPopup(View view, Context context,ArrayList<String> arrayList) {

        if(popupWindow!=null && popupWindow.isShowing()){
            popupWindow.dismiss();
        }

        mLayoutManager.setScrollEnabled(false);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //Making transparent layout

        final View popupView1  = inflater.inflate(R.layout.transparent_layout,
                (ViewGroup) view.findViewById(R.id.transprent));

        popupWindow1 = new PopupWindow(popupView1, WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow1.showAtLocation(popupView1, Gravity.CENTER, 0 , 0);
        popupWindow1.setBackgroundDrawable(new BitmapDrawable());
        popupWindow1.setOutsideTouchable(true);
        popupWindow1.showAsDropDown(popupView1, 0, 0);

        final View popupView  = inflater.inflate(R.layout.my_order_truck_popup_win,
                (ViewGroup) view.findViewById(R.id.popup_win));

        RecyclerView mRecyclerView = (RecyclerView) popupView.findViewById(R.id.truck_recyler_id);
        TextView textView=(TextView) popupView.findViewById(R.id.txt_truck);
        RelativeLayout cancl_rl=(RelativeLayout)popupView.findViewById(R.id.rl_cncl);
        textView.setText(context.getResources().getString(R.string.trucks));
        Config.colorFont(context,null,textView,null);
        popupView.setAnimation(AnimationUtils.loadAnimation(context, R.anim.pop_up_window));

        /********* Here is my PopUp window ********/
        Float m=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());
        int m1=Math.round(m);
        popupWindow = new PopupWindow(popupView,m1,WindowManager.LayoutParams.WRAP_CONTENT);

        popupWindow.showAtLocation(popupView, Gravity.CENTER, 0 , 0);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);

        MyOrderPopUPAdapter mAdapter = new MyOrderPopUPAdapter(context, arrayList);
        LinearLayoutManager LayoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(LayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter);
        onHandleCallBack.onHandleRequest(popupWindow,popupWindow1,"orderfragment");

        popupWindow1.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupWindow.dismiss();
                popupWindow1.dismiss();
                mLayoutManager.setScrollEnabled(true);
                return true;
            }
        });



        cancl_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                popupWindow1.dismiss();
                mLayoutManager.setScrollEnabled(true);

            }
        });

        popupWindow.showAsDropDown(popupView, 0, 0);
    }

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            onHandleCallBack = (OnHandleCallBack)context;

        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement onHandleCallBack");
        }
    }
Amit Ranjan
  • 195
  • 3
  • 15
  • what is popwindow extending from? – masoud vali Nov 16 '16 at 05:14
  • @masoudvali Popwindow is appear when i click button C in the Fragment B, programitically created like this PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); – Amit Ranjan Nov 16 '16 at 05:19
  • I know, I want to know its class is based on alertdialog or not – masoud vali Nov 16 '16 at 05:20
  • No , it's not based on the alertdialog , i want to know why fragment A is loading on the backpress – Amit Ranjan Nov 16 '16 at 05:22
  • What if you remove `if(popupWindow!=null && popupWindow.isShowing()){ popupWindow.dismiss(); backpopwindow.dismiss(); }` from `onBackPressed()` ? – Monish Kamble Nov 16 '16 at 05:24
  • From the fragment C when user press back button then , i had created the interface ,which is going to come to the Activity in the method onHandleRequest, in the onHandleRequest i am getting the popwindow instance , that instance i am passing to Activity onBackPressed – Amit Ranjan Nov 16 '16 at 05:28
  • @MonishKamble if i dismiss the popwindow in onHandleRequest,then when we come to fragment B and press the ButtonC ,then popwindow will appear then suddenly disappear. – Amit Ranjan Nov 16 '16 at 05:34
  • It would be better if you post more code. – Monish Kamble Nov 16 '16 at 05:38
  • @MonishKamble i had updated the code – Amit Ranjan Nov 16 '16 at 05:45

1 Answers1

0

Unlike AlertDialogs, PopupWindows are not implicitly dismissed on back press. We have to explicitly write a logic for it.

Change your onBackPressed() as below :

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

Here, we are first checking whether the PopupWindow is visible. And if it is visible, just dismiss the PopupWindow else allow the normal onBackPressed flow by calling super.onBackPressed().

Don't know the exact reason of you using PopupWindow. Check this out for more options and decide what suits your need :

Android Popup Window vs Android Dialog

Dialogs and Popups in Android

Community
  • 1
  • 1
Monish Kamble
  • 1,478
  • 1
  • 15
  • 28