-1

I am working on an android application where I use a custom view to my window. I have the following code to do it :-

private void systemOverlayFullScreen()
    {
        WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));

        //manager.removeView(view);

        WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();

        // changed to alerts or overlay from system_error
        localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;


        // set width and height of overlay originally -1
        localLayoutParams.width = -1;

        // changed gravity to bottom so as to hide the stop the home button press; originally -1
        localLayoutParams.height = -1;

        localLayoutParams.y = -getNavigationBarHeight();

        localLayoutParams.x = 0;

        localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

                // this is to enable the notification to recieve touch events
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

                // Draws over status bar
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

        // you can change it to transparent
        //localLayoutParams.format = PixelFormat.TRANSLUCENT;

        localLayoutParams.alpha = 0.3f;
        CustomViewGroup view = new CustomViewGroup(this);
        manager.addView(view, localLayoutParams);
    }

When I click the home button and then relaunch my app again, the previously added custom view is still present. I want to remove it when the app relaunches.

I have tried doing :-

@Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();


        if(view.getWindowToken() != null)
        {
            Toast.makeText(getApplicationContext(), "View present", Toast.LENGTH_SHORT).show();
             WindowManager manager = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE));

             manager.removeView(view);

        }
        else
        {
            Toast.makeText(getApplicationContext(), "View not present", Toast.LENGTH_SHORT).show();
        }
    }

But this doesnot work.

Can anyone tell me how to remove a view dynamically when the app starts ?

1 Answers1

0

Your code will execute exactly as specified when you open the app everytime. Meaning it will add the CustomView once the app is opened. The onRestart method takes into account the activity restart not the whole app. You could perform a check before adding the view. i.e the user enters there name and you store it SharedPreferences. Then you can test if the user signed up and if they did it means when theres a restart the CustomView won't be added to your window.

Aaron
  • 175
  • 2
  • 10
  • Thanks for your response, but I am calling my method to add view only from onPause of activity, and when activity is relaunched, I want to remove the view .. hence I put it inside the onRestart() – Tiken Maharaj May 23 '16 at 14:13
  • I misunderstood because you said relaunch the app. However I noticed in your method you have CustomViewGroup view = new CustomViewGroup(this); manager.addView(view, localLayoutParams); thats a local object to that method so what view are you trying to remove in the onRestart method? – Aaron May 23 '16 at 14:23
  • thats the same object/view I am trying to remove.. let me comment that out and make the view global and check .. thanks for pointing it out – Tiken Maharaj May 23 '16 at 14:25