Solution of flashing black screen when using surfaceview in Android fragment

The previous news client is reconstructed. All of them use fragment to switch pages. There is only one entry activity as the startup activity of the program. One interface needs to call the camera to identify the QR code, so surfaceview will be used for preview. The problem is that when switching to the corresponding fragment, the screen will be black, The normal interface will be displayed after about 1 second of blackout, and this phenomenon will only appear when you enter the fragment for the first time, and will not appear after entering. The solution is to inadvertently see it on GitHub. Try it and it will work. The solution is posted below.

Method 1: add the following code in oncreate of activity

But if you do this in fragment, it doesn't seem to work.

Method 2. Add a surfaceview element with length and width of 0px and invisible in the root view of the activity.

reason:

Different from the general view, surfaceview has its own good buffer and data access mechanism, and the system has special processing for it. When the surfaceview is added to the current activity for the first time, the system will rearrange the layout and relay to WindowManager. This will only appear for the first time. When the surfaceview is added later, the screen will not be black.

In my own project, only one activity is declared in the manifest file because all of them use fragments to switch pages, but whether to open a new activity as a fragment container can be configured in the code, because I don't want to add the code getwindow(). Setformat (PixelFormat. Transition) to this activity;, So I used the newly opened activity as the container for this fragment, and the problem was solved.

The code with a flash screen is like this

However, this function has an overloaded function, that is, whether to open a new activity. Here, the overloaded function is used to solve it

Function prototype

The above content is the solution to flash the black screen when switching using surfaceview in Android fragment shared by this article. I hope it will be helpful to you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>