Android Fragment's solution to using SurfaceView to switch is to flash a black screen

  • 2020-08-22 22:42:01
  • OfStack

Reconstructed before their own news 1 client, all use the Fragment to switch pages, only one entrance Activity Activity power as a procedure, including an interface needs to call camera 2 d code, then use SurfaceView for preview, then the problem comes, when the switch to the corresponding Fragment, screen will be black, 1 black 1 seconds showed normal interface, and this phenomenon is only the first to enter the Fragment will appear, will not be needed after entering, the solution is not on github saw, tried 1, It works. The solution is posted below.

Method 1. Add the following code to onCreate for Activity


getWindow().setFormat(PixelFormat.TRANSLUCENT); 

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

Method 2. Add 1 SurfaceView element to root view of activity with both length and width of 0px and no visible SurfaceView element.

The reason:

SurfaceView is different from the 1-like view, it has its own good buffering and data access mechanism, the system has special processing for it. When surfaceview is added to the current activity for the first time, the system will rearrange the layout of WindowManager, relayout, so that it will black out 1. This will only appear for the first time, and the screen will not be black when surfaceview is added later.

In my own project, only 1 activity was declared in the listing file because all the pages were switched by fragment. However, the code can be configured to open activity as the fragment container. Since I don't want to add the code getWindow().setFormat (PixelFormat.TRANSLUCENT) in this activity, I don't want to add getWindow(). , so I used the new activity as the container for the fragment, and the problem was solved.

The code with the flash screen looks like this


private static final int REQUEST_CODE_SCAN = 1;
openPageForResult("scan",null,CoreAnim.slide,REQUEST_CODE_SCAN);

However, this function has one overload function which is whether to open activity or not, which is solved here by using overload function


private static final int REQUEST_CODE_SCAN = 1;
openPageForResult(true,"scan",null,CoreAnim.slide,REQUEST_CODE_SCAN);

The function prototype


 /**
   *  Open the fragment And request the return value , And set whether to new activity In the open 
   * @param newActivity  Whether the new activity
   * @param pageName  The page name 
   * @param bundle  parameter 
   * @param coreAnim  animation 
   * @param requestCode  Request code 
   * @return  The open fragment object 
   */
  public final Fragment openPageForResult(boolean newActivity, String pageName, Bundle bundle, CoreAnim coreAnim, int requestCode);

The above content is Android Fragment SurfaceView switching using SurfaceView flash 1 black screen solution, I hope to help you.


Related articles: