SoFunction
Updated on 2025-04-07

Solve the screen (black screen flashes) and the solution to the black screen movement problem when Android switches to SurfaceView

1. In recent projects, there is an Activity that uses Fragment+ViewPager, one of which implements the video playback function, including SurfaceView. As a result, every time the program is opened, the screen will be black when it enters the activity for the first time. The reason lies in SurfaceView.

Detailed explanation:

I think I found the reason for the black flash. In my case I'm using a SurfaceView inside a Fragment and dynamically adding this fragment to the activity after some action. The moment when I add the fragment to the activity, the screen flashes black. I checked out grepcode for the SurfaceView source and here's what I found: when the surface view appears in the window the very fist time, it requests the window's parameters changing by calling a private (..) method. This method “gives” you a new frame, window, and window surface. I think the screen blinks right at that moment.
   The solution is pretty simple: if your window already has appropriate parameters it will not refresh all the window's stuff and the screen will not blink. The simplest solution is to add a 0px height plain SurfaceView to the first layout of your activity. This will recreate the window before the activity is shown on the screen, and when you set your second layout it will just continue using the window with the current parameters. I hope this helps.

reason:

Because SurfaceView is different from ordinary view, it has its own good buffering and data access mechanism, and the system has special processing for it. When the surfaceview is added on the current activity for the first time, the system will rearrange the layout of the WindowManager and relayout, which will make it black. This will only appear for the first time. When the surfaceview is added in the future, the screen will not be black.

solve:

You can add a SurfaceView to the file of other interfaces without entering the surfaceview interface (for example, many programs have a Loading interface as soon as they enter), and add a SurfaceView to the file of other interfaces, and make the width and height 0dp, which has no effect on the layout. Moreover, this thing makes the surfaceview appear for the first time, that is, the next time we actually use the SurfaceView, there will be no flashing screen.

There is a surfaceview playing video. When sliding the screen back and forth, the black screen movement problem is about

solve:

(true);

().setFormat();

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!