【问题标题】:Remove custom-view when application is relaunched android重新启动应用程序时删除自定义视图android
【发布时间】:2016-05-23 13:51:09
【问题描述】:

我正在开发一个 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();
        }
    }

但这不起作用。

谁能告诉我如何在应用启动时动态删除视图?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复,但我正在调用我的方法以仅从活动的 onPause 添加视图,并且当重新启动活动时,我想删除视图..因此我将其放在 onRestart( )
    • 我误解了,因为您说重新启动应用程序。但是我注意到在你的方法中你有 CustomViewGroup view = new CustomViewGroup(this); manager.addView(view, localLayoutParams);那是该方法的本地对象,那么您要在 onRestart 方法中删除什么视图?
    • 这是我要删除的同一个对象/视图.. 让我评论一下,使视图全局并检查.. 感谢您指出它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    相关资源
    最近更新 更多