简体   繁体   中英

Keep Android Popup Windows always on top , even though activity goes in background and new activity comes back

This is a popup window which is being shown from an activity A. User presses button on Activity A then Activity B comes on top. But problem is popup also goes behind activity B.

I want pop up to be always on top even though activity goes in back ground

 LayoutInflater layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

LeadOffPopView = layoutInflater.inflate(
        R.layout.leadoffdetect, null);
LeadOffPopWindow = new PopupWindow(
        LeadOffPopView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView bLATxtVw = (TextView) (LeadOffPopView)
        .findViewById(R.id.LATxtVw);
final TextView bRATextView = (TextView) (LeadOffPopView)
        .findViewById(R.id.RATxtVw);
final TextView bLLTextView = (TextView) (LeadOffPopView)
        .findViewById(R.id.LLTxtVw);
final TextView bRLTextView = (TextView) (LeadOffPopView)
        .findViewById(R.id.RLTxtVw);
final TextView bV1TextView = (TextView) (LeadOffPopView)
        .findViewById(R.id.V1TxtVw);
final TextView bV2TextView = (TextView) (LeadOffPopView)
        .findViewById(R.id.V2TxtVw);
final TextView bV3TextView = (TextView) (LeadOffPopView)
        .findViewById(R.id.V3TxtVw);
final TextView bV4TextView = (TextView) (LeadOffPopView)
        .findViewById(R.id.V4TxtVw);
final TextView bV5TextView = (TextView) (LeadOffPopView)
        .findViewById(R.id.V5TxtVw);
final TextView bV6TextView = (TextView) (LeadOffPopView)
        .findViewById(R.id.V6TxtVw);


final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        if (bLAOff) {
            bLATxtVw.setVisibility(View.VISIBLE);
        } else
            bRATextView
            .setVisibility(View.INVISIBLE);

        if (bRAOff) {
            bRATextView
            .setVisibility(View.VISIBLE);
        } else
            bRATextView
            .setVisibility(View.INVISIBLE);
        if (bLLOff) {
            bLLTextView
            .setVisibility(View.VISIBLE);
        } else
            bLLTextView
            .setVisibility(View.INVISIBLE);

        if (bRLOff) {
            bRLTextView
            .setVisibility(View.VISIBLE);
        } else
            bRLTextView
            .setVisibility(View.INVISIBLE);

        if (bV1Off) {
            if(!MainClass.bLANDSCAPEENABLED)
            bV1TextView
            .setVisibility(View.VISIBLE);
        } else
            bV1TextView
            .setVisibility(View.INVISIBLE);

        if (bV2Off) {
            if(!MainClass.bLANDSCAPEENABLED)
            bV2TextView
            .setVisibility(View.VISIBLE);
        } else
            bV2TextView
            .setVisibility(View.INVISIBLE);

        if (bV3Off) {
            if(!MainClass.bLANDSCAPEENABLED)
            bV3TextView
            .setVisibility(View.VISIBLE);
        } else
            bV3TextView
            .setVisibility(View.INVISIBLE);

        if (bV4Off) {
            if(!MainClass.bLANDSCAPEENABLED)
            bV4TextView
            .setVisibility(View.VISIBLE);
        } else
            bV4TextView
            .setVisibility(View.INVISIBLE);

        if (bV5Off) {
            if(!MainClass.bLANDSCAPEENABLED)
            bV5TextView
            .setVisibility(View.VISIBLE);
        } else
            bV5TextView
            .setVisibility(View.INVISIBLE);

        if (bV6Off) {
            if(!MainClass.bLANDSCAPEENABLED)
            bV6TextView
            .setVisibility(View.VISIBLE);
        } else
            bV6TextView
            .setVisibility(View.INVISIBLE);

        handler.postDelayed(this, 500);
    }
}, 250);

LeadOffPopWindow.showAtLocation(
        LeadOffPopView,
        Gravity.CENTER_HORIZONTAL | Gravity.TOP,20 ,
        300);

PopUpWindow/Dialog is attached with your Activity WindowManager context, so when ever Your activity will go in background , PopUpWindow/Dialog will also get hide.

Therefore Please Use System Alert PopupWindow/Dialog for achieve this, you can create system dialog using below code:

AlertDialog newDialog = new AlertDialog.Builder(context)
                    .setMessage(text)
                    .setPositiveButton(R.string.ok, null)
                    .setCancelable(true)
                    .create();
newDialog.getWindow().setType(
                    WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
newDialog.getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_DIM_BEHIND);
newDialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM