Solution to flash black screen when using Surfaceview switch in Android fragment _android

Source: Internet
Author: User

Refactoring the next one of their own news clients, all using fragment for page switching, only one entry activity as the start of the program activity, one of the interface needs to call the camera to identify two-dimensional code, and then use the Surfaceview to preview, So here's the problem, when switching to the corresponding fragment, the screen will be black, black for about 1 seconds to show the normal interface, and this phenomenon is only the first time to enter the fragment will appear, after the entry will not appear, the solution is not intended to see on the GitHub, tried, It can be done through, and the solution is posted below.

Method One, add the following code in the OnCreate of the activity

Copy Code code as follows:

GetWindow (). SetFormat (pixelformat.translucent);

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

Method Two, in root view of activity add a long width is 0px and invisible surfaceview element.

Reason:

Surfaceview because it is different from the general view, it has its own good buffering and data access mechanism, the system has special treatment for him. When Surfaceview first in the current activity to add, the system will rearrange the layout of the WindowManager, Relayout, this will be black, this will only appear in the first, later add Surfaceview will not be black screen.

In your own project, because all use fragment to switch pages, only one activity is declared in the manifest file, but the code can configure whether or not new activity is opened as a fragment container, Since I don't want to add code GetWindow (). SetFormat (pixelformat.translucent) To this activity, I used the new activity as the container for this fragment, so the problem was solved.

The code with the splash screen is like this.

private static final int request_code_scan = 1;
Openpageforresult ("Scan", Null,coreanim.slide,request_code_scan);

But this function has an overloaded function that is whether to start a new activity, using overloaded functions to solve

private static final int request_code_scan = 1;
Openpageforresult (True, "scan", Null,coreanim.slide,request_code_scan);

Function prototypes

 /**
   * Open fragment and request a return value and set whether to open in the new activity
   * @param newactivity whether the new activity
   * @param pagename page name
   * @param bundle Parameter
   * @param coreanim animation
   * @param requestcode Request Code
   * @return Open Fragment object/
   public
  FINA L Fragment Openpageforresult (boolean newactivity, String pagename, Bundle Bundle, Coreanim coreanim, int requestcode);

The above content is this article gives everybody to share the Android fragment to use Surfaceview to flash a black screen solution, hope to be helpful to everybody.

Related Article

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.