Вопрос

When I open the app it shows a white screen for a few seconds for starting the service and all other things.

Can I avoid it by using a custom boot/loading screen loader or any other method is possible? and where I can put that code?

Help me, Thanks advance..

Это было полезно?

Решение

Its happening because of you may be doing some the time consuming task in application UI thread. You have to do that task using Async or separate thread.

Whenever you are trying to do some time consuming tasks in your application's main UI thread it always blocks the UI.

You can also use the Handler. Android provides additional constructs to handle concurrently in comparison with standard Java. You can use the android.os.Handler class or the AsyncTasks classes.

The AsyncTask class encapsulates the creation of a background process and the synchronization with the main thread. It also supports reporting progress of the running tasks.

   class task extends AsyncTask<String, Integer, String> {
           @Override
    protected String doInBackground(String... params) {
             //your logic 
                 return null;
              }
            }

For more details Check

Другие советы

@grlsHu answer is absolutely correct. Additionally I recommend to start the Background Tasks after the first screen is shown. This can be the Apps main Activity or - if it makes no sense without data and content - a splash screen. In contrast, if you start the background too early, it would run concurrently to th UI Thread and showing first screen would still take quite long.

This means:

  • Put your startup tasks in a separate Thread using AsyncTask as suggested by @grlsHu.
  • Create an Activity that can be shown without data or content from the rest of your app. (Splashscreen)
  • In the Manifest take the Splashscreen as the one to launch.
  • At the end of onResume() of the Splashscreen start the background tasks with AsyncTask.execute(...).
  • At the end of the AsyncTask with AsyncTask.onPostExecute() finish the Splashscreen an start the main Activity of your app.

With this approach you should have a direct reaction for the user and get all your services started.

Look this How to avoid black screen in android while my app is loading? This give me an idea to make a custom theme like flash and it solved my problem. For those who are looking for same answer this is how i do it It worked for me hope it help you guys too. In style.xml.

   <style name="MySplashTheme" parent="AppBaseTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@drawable/splash_image</item>
   </style>

Manifest file in activity tag:

android:theme="@style/MySplashTheme"

Well this is not the proper solution, the proper solution is to use async task or handler. And do things in separate thread.

This will help you put this in your style.xml

<item name="android:windowDisablePreview">true</item>

You can add loader(in thread) in oncreate method of application .

Yeah, actually it is about your launcher activity besides your application.

If you don't do anything in your application class (by the way, if you do then other answers will probably work for you), then you should consider putting a splash activity, which is lighter than your launcher activity such as with only background progress or something so it will be much more easier to load that screen.

Or you can also set windowBackground in style, so it will display at least your background until application will load instead of white screen.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top