当应用程序重新启动android时删除自定义视图

时间:2016-05-23 13:51:09

标签: android android-custom-view android-windowmanager

我正在开发一个Android应用程序,我在窗口中使用自定义视图。我有以下代码: -

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);
    }

当我单击主页按钮然后再次重新启动我的应用程序时,之前添加的自定义视图仍然存在。我想在应用重启时将其删除。

我尝试过: -

@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();
        }
    }

但这不起作用。

有人可以告诉我如何在应用启动时动态删除视图吗?

1 个答案:

答案 0 :(得分:0)

每次打开应用程序时,您的代码将完全按照指定执行。这意味着它将在应用程序打开后添加CustomView。 onRestart方法考虑了活动重启而不是整个应用程序。您可以在添加视图之前执行检查。即用户输入名称并将其存储为SharedPreferences。然后,您可以测试用户是否已注册,如果他们这样做,则意味着当重新启动时,CustomView将不会添加到您的窗口。