Black screen runtime setContentView

I have MainActivity . Sometimes, when it boots up, I watch a black screen for a second. I measured the timings for operations in the onCreate method and found that for setContentView(R.layout.main_screen); it took more than one second, I prefer to show the previous screen (in my case the Splash screen) instead of this black screen during the execution of setContentView . How can I get rid of this black screen?

It seems that the android somehow precedes the layouts, and sometimes such problems arise. But if I kill my process and run the application, I always see this black screen.

+2
source share
1 answer
  • Use a static variable to process the View cache.
  • Use AsyncTask not to slow down the beginning of Activity
  • Use LayoutInflater to inflate View layout and cache
  • In onCreate() target activity, set the cache

Something like that:

Origin of activity

 ... //noinspection unchecked new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { LayoutInflater inflater = (LayoutInflater) MainParentActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // VERY VERY SLOW action if your Activity is heavy DialerActivity.cachedView = inflater.inflate(R.layout.dialer_portrait, null, false); return null; } @Override protected void onPostExecute(Void aVoid) { Intent intent = new Intent(MainParentActivity.this, DialerActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); } }.execute(); ... 

Target activity

 public class DialerActivity extends MainParentActivity { static View cachedView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (cachedView != null) { setContentView(cachedView); } else { setContentView(R.layout.dialer_portrait); } } . . . 

You can also use the ProgressDialog during bloat to avoid freezing on transition.

+2
source

All Articles