Cannot remove a view attached by windowManager.addView()
Asked Answered
J

1

5

I'm trying to remove a layer added to WindowManager. But nothing happens when I call removeView(). Does anybody know how to remove it? My code looks like this.

public class MainActivity extends Activity implements View.OnClickListener{

    private WindowManager wm;
    private  WindowManager.LayoutParams orientationLayout;
    private LinearLayout orientationChanger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // init landscape things
        wm = (WindowManager) getApplicationContext().getSystemService(Service.WINDOW_SERVICE);
        orientationChanger = new LinearLayout(getApplicationContext());
        orientationChanger.setClickable(false);
        orientationChanger.setFocusable(false);
        orientationChanger.setFocusableInTouchMode(false);
        orientationChanger.setLongClickable(false);
        orientationLayout = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.RGBA_8888);
        orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

        // set view
        setContentView(R.layout.calibrate);

        findViewById(android.R.id.button).setOnClickListener(this);

        lockLandScape();
    }

    public void lockLandScape(){
        wm.addView(orientationChanger, orientationLayout);

        orientationChanger.setVisibility(View.GONE);
        orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        wm.updateViewLayout(orientationChanger, orientationLayout);
        orientationChanger.setVisibility(View.VISIBLE);
    }

    public void releaseLandScape(){
        wm.removeView(orientationChanger);

        // This doesn't work as well
        //wm.removeViewImmediate(orientationChanger);
    }

    @Overrride
    public void onClick(View view){
        Log.i("myapp", "clicked")
        releaseLandScape();
    }

}
Jehad answered 1/8, 2013 at 13:10 Comment(1)
You may have a look at https://mcmap.net/q/1789788/-can-39-t-remove-overlay-view I had a similar problemQuartas
R
14

Instead of using an attribute like wm, could you try using

(WindowManager) getApplicationContext().getSystemService(Service.WINDOW_SERVICE)

everytime ?

like :

((WindowManager) getApplicationContext().getSystemService(Service.WINDOW_SERVICE)).removeView(orientationChanger);
Resplendence answered 3/10, 2013 at 2:46 Comment(2)
Is there any reason why this would work ? And not the attribute implementation ? @ResplendenceSwordplay
This is the correct solution!! Maybe WindowManager can not be 'reused' safely in all cases / situation?Kilburn

© 2022 - 2024 — McMap. All rights reserved.