There are two relativeLayout

  1. splashRelativeLayout
  2. playzoneRelativeLayout

I wrote such code that splashRelativeLayout came out as a splash at the beginning of the application download for 5 seconds.

@Override protected void onResume() { super.onResume(); splashRelativeLayout.setVisibility(View.VISIBLE); playzoneRelativeLayout.setVisibility(View.INVISIBLE); new Handler().postDelayed(new Runnable() { @Override public void run() { splashRelativeLayout.setVisibility(View.INVISIBLE); playzoneRelativeLayout.setVisibility(View.VISIBLE); } },5000); } 

But, before splashRelativeLayout appears on the screen, the program shows a white window - waits for the entire application to load and only then shows splashRelativeLayout.
How to make the application immediately show splashRelativeLayout at first, and then only slowly download all the other layouts.

1 answer 1

Make a separate SplashActivity, which will be LAUNCHER in the manifest:

  <activity android:name=".ui.splash.SplashActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

Show your splash layout in it:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splashRelativeLayout); } 

Well, in onResume list your handler with a delayed display:

 @Override protected void onResume() { super.onResume(); // запуститС ΠΎΡΠ½ΠΎΠ²Π½ΡƒΡŽ Π°ΠΊΡ‚ΠΈΠ²ΠΈΡ‚ΠΈ послС Π·Π°Π΄Π΅Ρ€ΠΆΠΊΠΈ new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(SplashActivity.this, MainActivity.class); SplashActivity.this.startActivity(intent); SplashActivity.this.finish(); // ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΊΠ°ΠΊΡƒΡŽ-Π»ΠΈΠ±ΠΎ Π°Π½ΠΈΠΌΠ°Ρ†ΠΈΡŽ ΠΏΡ€ΠΈ Π·Π°ΠΊΡ€Ρ‹Ρ‚ΠΈΠΈ сплэш - ΠΊ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Ρƒ, исчСзновСниС SplashActivity.this.overridePendingTransition(R.anim.fade_in_activity, R.anim.fade_out_activity); } },5000); } 

res / anim / fade_in_activity.xml

 <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" /> 

res / anim / fade_out_activity.xml

 <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.0" android:fillAfter="true" android:duration="1000" /> 
  • And removeCallback for Handler do not need to call? No problems with memory leaks? After the handler displays the MainActivity, will the handler automatically die? - androman
  • @androman will be no problem. - mit
  • there will be problems, so call - tim_taller
  • @tim_taller, activity with handler will die after finish - Flippy
  • Ie at the end of each handler is better to call finish, so that the handler will die for sure? - androman