Hello. In onCreate() I do some simple actions. onCreate() launching the application, first there is a white screen for 5 seconds somewhere, and then the design is shown. Why does this happen and how to fix it?

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.header1 = (LinearLayout) findViewById(R.id.header1); this.header2 = (LinearLayout) findViewById(R.id.header2); this.header3 = (LinearLayout) findViewById(R.id.header3); this.header4 = (LinearLayout) findViewById(R.id.header4); this.header5 = (LinearLayout) findViewById(R.id.header5); this.header6 = (LinearLayout) findViewById(R.id.header6); this.header7 = (LinearLayout) findViewById(R.id.header7); this.header8 = (LinearLayout) findViewById(R.id.header8); this.header9 = (LinearLayout) findViewById(R.id.header9); this.header10 = (LinearLayout) findViewById(R.id.header10); this.header11 = (LinearLayout) findViewById(R.id.header11); this.header12 = (LinearLayout) findViewById(R.id.header12); this.header13 = (LinearLayout) findViewById(R.id.header13); this.header14 = (LinearLayout) findViewById(R.id.header14); this.header15 = (LinearLayout) findViewById(R.id.header15); this.header16 = (LinearLayout) findViewById(R.id.header16); this.header17 = (LinearLayout) findViewById(R.id.header17); this.header18 = (LinearLayout) findViewById(R.id.header18); this.header19 = (LinearLayout) findViewById(R.id.header19); this.header20 = (LinearLayout) findViewById(R.id.header20); } 
  • Show your onCreate() - Barmaley
  • In onCreate I find the item by id from xml. There are 20 of them - Ivan
  • Wait for markup to be set in setContentView (). This is before you search for items by id. Show the code. - Kirill Malyshev
  • Code pliz - do not be lazy :) - Barmaley
  • this.header = (LinearLayout) findViewById (R.id.header); and such 20 pieces - Ivan

2 answers 2

The markup inflate occurs by the setContentView() method - it is the one that reads the XML file and on its basis builds an image on the screen. That is, the result of the execution of this method is an image, therefore the actions after this method do not affect the display speed.

Considering how many containers you’re looking for afterwards (and they certainly still contain a large number of widgets in themselves), it’s not difficult to assume that inflating such a markup takes a fair amount of time.

You need to optimize your xml markup, make it simpler and more linear. To get started, it is necessary to "run" through the Hierarchy Viewer (currently it is not a separate file, but is included in the Android Studio tools ) to identify problem areas, then think how to solve these problems. All the "lights" of all the markup elements should ideally be green, in extreme cases - yellow. There are no universal solutions here, but there are general recommendations for optimizing markup , and also here.

  • Ok, I'll try. Unfortunately, I don’t throw anything out of the markup already. I optimized everything. - Ivan
  • one
    @Ivan I, for example, find it difficult to imagine markup in which 20 containers are really needed - pavlofff

In general, even when marking with one RelativeLayout received a white screen for 2-3 seconds. The solution was found very simple.

In colors.xml add background color

 <color name="background">#000000</color> 

In styles.xml

 <item name="android:windowBackground">@color/background</item> 

Strange why no one knew about it.

  • Changing the background color will not speed up the appearance of the widgets, if you had to remove the white color, then you had to write about it in the question. - pavlofff
  • @ pavlofff question and how to remove the white screen. What is the markup with one RelativeLayout that my big one loads almost the same. - Ivan