Question

i made a small app where i load a new view with TYPE_SYSTEM_OVERLAY and now i want to close it by click to a button and i got the error:

Activity com.example.viewandbutton.View2Class has leaked window android.widget.RelativeLayout@44957168 that was originally added here

i already searched for the error and found out that i have to dismiss() the activity but dismiss only works for a Dialog object, so i have no idea how to stop the activity.

here is my code:

public class View2Class extends Activity {

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.view2);

           LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
           View myView = inflater.inflate(R.layout.view2, null);

           WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    PixelFormat.TRANSLUCENT);

           final WindowManager wm = (WindowManager) this.getSystemService(WINDOW_SERVICE);
           wm.addView(myView, params);

           Button btnActivity = (Button)findViewById(R.id.button2);
           btnActivity.setOnClickListener(new View.OnClickListener() 
            {   
                @Override
                public void onClick(View v) {
                finish();
                }
            });

        }    
}

thanks!

Was it helpful?

Solution

As per my opinion Remove your View on button onClick() before the finish() like:

 btnActivity.setOnClickListener(new View.OnClickListener() 
        {   
            @Override
            public void onClick(View v) {
            wm.removeView(myView);
            finish();
            }
        });

OTHER TIPS

here is the complete working code …

public class View2Class extends Activity {

    View myView;
    WindowManager wm;

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.view2);

           LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
           myView = inflater.inflate(R.layout.view2, null);

           WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                   WindowManager.LayoutParams.WRAP_CONTENT,
                   WindowManager.LayoutParams.WRAP_CONTENT,
                   WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                   WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                           | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                   PixelFormat.TRANSLUCENT
                   );

           final WindowManager wm = (WindowManager) this.getSystemService(WINDOW_SERVICE);
           wm.addView(myView, params);


           Button bb = (Button) myView.findViewById(R.id.button2);
           bb.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                wm.removeView(myView);
                    finish();
               }
           });

     }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top