简体   繁体   English

Android中的屏幕锁定和主页按钮之间的区别

[英]Difference between screen lock and home button in Android

I do not understand the differences between when a user locks the screen (using the top screen lock button) and immediately returns to the application vs. when the user presses the home button and then immediately returns to the application. 我不了解用户锁定屏幕(使用顶部屏幕锁定按钮)立即返回到应用程序与用户按下主页按钮然后立即返回到应用程序之间的区别。

It seems that all the same calls are being made. 似乎正在进行所有相同的调用。 From my observations: 根据我的观察:

Called when home button or screen lock are pressed: onPause -> onStop 当按下主页按钮或屏幕锁定时调用:onPause-> onStop

Called when application is pressed after home button or screen lock is re-pressed: onRestart -> onStart -> onResume 在按下主页按钮或重新锁定屏幕后按下应用程序时调用:onRestart-> onStart-> onResume

My individual problem: 我的个人问题:

This is particularly causing me greif because I am recreating a SurfaceView and a GLSurfaceView to a FrameLayout upon onResume, however, depending on the button pressed, the ordering of the elements is getting changed. 这特别让我感到吃惊,因为我在onResume上重新创建了SurfaceView和GLSurfaceView到FrameLayout,但是,根据所按下的按钮,元素的顺序正在更改。 I have the following code in my onResume: 我的onResume中有以下代码:

cameraPreviewArea = (FrameLayout) findViewById(id.camera_preview);
cameraPreviewArea.addView(glView, glLayout);
cameraPreviewArea.addView(camprevSurfaceView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

This has the effect of displaying my glSurfaceView on top in the following situations: the first time the app has launched, and when the app is being resumed from being screen-locked and then screen-unlocked. 在以下情况下,这具有在顶部显示我的glSurfaceView的效果:第一次启动应用程序,以及从屏幕锁定然后屏幕解锁的状态恢复应用程序的时间。 However, upon pressing the home button, and then reopening the application, the SurfaceView is being placed ON TOP of the glSurfaceView! 但是,按下主屏幕按钮,然后重新打开应用程序时,SurfaceView将被放置在glSurfaceView的顶部!

If I switch the addView calls as follows, the opposite situations will occur. 如果按以下方式切换addView调用,则会发生相反的情况。 I could fix this with some boolean flag, but it it unclear where I would set the boolean because of my uncertainty as to the difference between a screen lock/unlock and the home button. 我可以使用一些布尔标志来解决此问题,但是由于不确定屏幕锁定/解锁和主屏幕按钮之间的差异,因此我不确定在哪里设置布尔值。 Also, I do not want to solve the problem in this manner anyway because it seems hacky and lacks any real understanding of the problem. 另外,我也不想以这种方式解决问题,因为它看起来很笨拙,并且对问题没有任何真正的了解。

Thank you in advance! 先感谢您!

In general, there is no reason you should need to constantly add and remove views from the hierarchy of your Activity , and removing this code will make your application more consistent. 通常,没有理由不需要从Activity的层次结构中不断添加和删除视图,而删除此代码将使您的应用程序更加一致。

Since both of the views you are interested in are SurfaceView components, if there is some action that you need to take when the window becomes visible or hidden, you can take advantage of the SurfaceHolder.Callback to monitor the onSurfaceCreated() and onSurfaceDestroyed() methods. 由于您感兴趣的两个视图都是SurfaceView组件,因此,如果在窗口变为可见或隐藏时需要采取一些措施,则可以利用SurfaceHolder.Callback来监视onSurfaceCreated()onSurfaceDestroyed()方法。

This specific situation - using two surfaceViews and specifying their Z order within a window - does not seem to currently be supported by Android. Android目前似乎不支持这种特定情况-使用两个surfaceView并在窗口中指定其Z顺序。 This thread over at the android developer group shares the following information: android开发人员组中的该线程共享以下信息:

Multiple active overlapping surface views, of any sort, are not currently supported by the framework. 框架当前不支持任何形式的多个活动重叠表面视图。 You may get them to work, but it is mostly due to luck -- the view hierarchy does not define the Z-ordering of those surfaces, nor try to ensure they are Z-ordered in any particular way, so this may change for whatever reason. 您可以使它们工作,但这主要是因为运气好- 视图层次结构未定义这些曲面的Z顺序,也未尝试确保它们以任何特定方式按Z顺序排列,因此无论发生什么变化原因。

Well... there you go! 好吧...你去了!

...But for anyone attempting this I found a workaround: make your camera preview size a tiny 1x1 square. ...但是对于任何尝试这样做的人,我发现了一种解决方法:将您的相机预览尺寸缩小为1x1平方。 This will allow you to display both simultaneously (because a camera preview must be visible in order for the preview to continue) and ignore the pesky SurfaceView issues that the cameraPreview presents you with. 这将允许您同时显示这两个图像(因为必须先显示照相机预览才能继续预览),并且可以忽略显示CameraPreview会给您带来的烦人的SurfaceView问题。 I believe there is a better solution using strictly one GLSurfaceView, but it is not compatible below 3.0. 我相信使用严格的一个GLSurfaceView会有更好的解决方案,但是在3.0以下不兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM