Android display pop-up screen at boot time

I have an Android app that shows “Splash Screen” for 3 seconds. After that, MainActivity is loaded.

Unfortunately, MainActivity loads an extra ~ 4 seconds. At the first start, even more. However, when the application is downloaded, everything runs smoothly.

Now, how can I achieve that MainActivity loads while the Splash screen is displayed? It just needs to display the image until all of this is fully loaded. I read about Async-Task, but I'm not sure where to place it and how to use it correctly. Can someone help me?

SplashScreen.java

public class SplashScreen extends Activity { private static int SPLASH_TIME_OUT = 3000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_startup); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent i = new Intent(SplashScreen.this, MainActivity.class); startActivity(i); finish(); } }, SPLASH_TIME_OUT); } } 

MainActivity.java

 public class MainActivity extends Activity implements OnClickListener, MediaController.MediaPlayerControl { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Some heavy processing //starting services //starting Google Text to Speech //and so on... } } 
+6
source share
4 answers

If there are no specific restrictions on the display time of the splash screen, you can use AsyncTask as follows:

 public class SplashScreen extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_startup); startHeavyProcessing(); } private void startHeavyProcessing(){ new LongOperation().execute(""); } private class LongOperation extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { //some heavy processing resulting in a Data String for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.interrupted(); } } return "whatever result you have"; } @Override protected void onPostExecute(String result) { Intent i = new Intent(SplashScreen.this, MainActivity.class); i.putExtra("data", result); startActivity(i); finish(); } @Override protected void onPreExecute() {} @Override protected void onProgressUpdate(Void... values) {} } } 

If the resulting if data is of a different nature than String, you can place the Parcelable as an extra for your activity. In onCreate you can get the data with:

getIntent().getExtras.getString('data');

+7
source

You should not create a new thread at startup, instead you should create a view that should not wait for your resources to load, as described in this article: The screensaver paints the correct path .

As stated in the article, you should create a layer-list drawable instead of the XML layout file:

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Fill the background with a solid color --> <item android:drawable="@color/gray"/> <item> <!-- Place your bitmap in the center --> <bitmap android:gravity="center" android:src="@mipmap/ic_launcher"/> </item> </layer-list> 

Then create a theme using the drawn file as the background. I use the background attribute instead of the windowBackground attribute, as suggested in the article, because background takes into account the status and navigation panels, improving the selection. I also set windowAnimationStyle to null so that the splash screen does not animate the transition to MainActivity :

 <resources> <!-- Base application theme --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> </style> <!-- Splash Screen theme --> <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:background">@drawable/background_splash</item> <item name="android:windowAnimationStyle">@null</item> </style> </resources> 

Then declare your theme in the manifest for your SplashActivity :

 <activity android:name=".SplashActivity" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

And finally, all you need to do in SplashActivity will launch your MainActivity , and the splash screen will only show until you need to configure the application:

 public class SplashActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); } } 
+15
source

your screensaver code works fine, but when you call the next activity, then in onCreate () use Asynctask for your heavy tasks ...

0
source

How about for the sake of simplicity you combine your activity with your main activity? Thus, you get the best of both worlds, namely the splash screen when your data is being prepared for the first time, and the quick launch when it was prepared earlier. Making the user wait for anything is not a very good form ...

Sort of:

 public class MainActivity extends Activity implements OnClickListener, MediaController.MediaPlayerControl { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initially shows splash screen, the main UI is not visible setContentView(R.layout.activity_main); // Start an async task to prepare the data. When it finishes, in // onPostExecute() get it to call back dataReady() new PrepareDataAsyncTask(this).execute(); } public void dataReady() { // Hide splash screen // Show real UI } } 
0
source

All Articles