Android solves the problem of flashing a black screen during SurfaceView switching in Fragment.

Source: Internet
Author: User

Android solves the problem of flashing a black screen during SurfaceView switching in Fragment.

After restructuring a previous news client, Fragment is all used for page switching. There is only one portal Activity as the startup Activity of the program. One of the interfaces needs to call the camera to identify the QR code, as a result, SurfaceView will be used for preview. The problem arises. When you switch to the corresponding Fragment, the screen will be black and the normal interface will be displayed in about one second after it is black, in addition, this phenomenon only occurs when you enter the Fragment for the first time, and will not appear after you enter the Fragment. The solution is that you did not see it on github. After trying it, you can do it, the solution is as follows.

Method 1: Add the following code in onCreate of Activity

getWindow().setFormat(PixelFormat.TRANSLUCENT);  

However, if you do this in fragment, it seems that it will not work.

Method 2: add an element with a length and width of 0 Px to the root view of the activity and the SurfaceView element is invisible.

Cause:

SurfaceView is different from a general view. It has its own buffer and data access mechanism, and the system has special processing for it. When surfaceview is added to the current activity for the first time, the system will re-arrange the layout and relayout for WindowManager. This will make it black and appear only for the first time, when surfaceview is added later, the screen will not be black.

In my own project, because all the pages are switched using fragment, only one activity is declared in the list file, but the code can configure whether the newly opened activity is used as the fragment container, because I don't want to add code to this activity any moreGetWindow (). setFormat (PixelFormat. TRANSLUCENT );So I used the newly opened activity as the fragment container, and the problem was also solved.

The code with a pop-up screen is like this.

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

However, this function has an overload function that determines whether to create a new activity. Here we use the overload function to solve this problem.

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

Function prototype

/*** Open the fragment and request the returned value, and set whether to open * @ param newActivity * @ param pageName Page name * @ param bundle parameter * @ param coreAnim animation * @ param requestCode Request Code * @ return fragment object */public final Fragment openPageForResult (boolean newActivity, string pageName, Bundle bundle, CoreAnim coreAnim, int requestCode );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.