Lazar Radosavljevic
Lazar Radosavljevic

Reputation: 19

PopUp window won't dismiss on back button

I am trying the whole day to figure out why the pop up won't dismiss. I read a lot of answers on the internet, but nothing worked.

Here is my code:

Initialization:

LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.pop_up,
(ViewGroup) findViewById(R.id.popup_element));
mPopUp = new PopupWindow(layout, mScreenWidth, mScreenHeight, true);
mPopUp.showAtLocation(layout, Gravity.CENTER, 0, 0);
mPopUp.setBackgroundDrawable(new ShapeDrawable());

On back pressed:

public void onBackPressed() {
if(mPopUp!=null){
mPopUp.dismiss();
}
else{
super.onBackPressed();
}
}

I really don't know what to do.
I read somewhere that I should put mPopUp.setBackgroundDrawable(new ShapeDrawable()); after initialization but no luck. I have tried everything.

Thank you in advance.

EDIT: In my log can I get an error:

Access to extended visibility flags denied: Requires com.sonymobile.permission.SYSTEM_UI_VISIBILITY_EXTENSIONS permission.

Upvotes: 2

Views: 1230

Answers (1)

Zoe - Save the data dump
Zoe - Save the data dump

Reputation: 28229

I preffer using DialogFragment because it allows for more customizability.

public class Popup extends DialogFragment implements View.OnClickListener {
    Context c;

    public static Popup newInstance() {
        Popup f = new Popup ();

        // Supply num input as an argument.
        Bundle args = new Bundle();

        f.setArguments(args);

        return f;
    }

    public void params(Context c){
        this.c = c;
    }




    @Override
    public void onCreate(Bundle sis){
        super.onCreate(sis);

        int style, theme;

        style = DialogFragment.STYLE_NO_FRAME;
        theme = android.R.style.Theme_Holo_Dialog;


        setStyle(style, theme);

    }





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.stats, container, false);
        this.v = v;
        setButtons();
        return v;
    }



    private void setButtons(){
        //set up all buttons, textviews etc
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                dismiss();
                break;


        }

    }
}

For calling the whole thing:

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment prev = getFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    PopupnewFragment = Popup.newInstance();
    newFragment.params(getBaseContext(), clicker, this);
    newFragment.show(ft, "dialog");

Now, for the error you recieved:

Sony is a really odd manufacturer. They have created their own permissions that doesn't care about what the base system perms have allowed. The UI visibility permission by SOny is reqired because you are probably testing with a sony phone.

Why do sony have their own permissions?

Because every phone that isn't nexus is Android with modifications. Every single manufacturer creates their own slightly modified version of the OS so they can get in their own apps onto the phone without them having to be installed by the user.

So if you want to solve the error, you have to add that permission so Sony phones can use your app. Alternativly, you can block Sony's accessability to the app or you could try an alternative way like the one I described above

Upvotes: 1

Related Questions