2

I am developing an application in which i am getting white screen before splash screen when ever i run the app.

On splash screen i created database in background also i am having the push notification registration code. For push notification registration i refer this link. So my code of splash screen code is as follows:

public class SplashScreenActivity extends AppCompatActivity {
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    private BroadcastReceiver mRegistrationBroadcastReceiver;

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

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
                boolean sentToken = sharedPreferences.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
                if (sentToken) {
                    // TODO token sent to server
                } else {
                    // TODO show error that token not sent to server
                }
            }
        };

        if (checkPlayServices()) {
            // Start IntentService to register this application with GCM.
            Intent intent = new Intent(this, RegistrationIntentService.class);
            startService(intent);
        }

        InitializeScreen();
    }
    private boolean checkPlayServices() {
        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (apiAvailability.isUserResolvableError(resultCode)) {
                apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.i("Splash screen activity", "This device is not supported.");
                finish();
                }
            return false;
        }
        return true;
    }

    @Override
    protected void onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
    }

    private void InitializeScreen() {
        new LoadDataBase(SplashScreenActivity.this).execute(SplashScreenActivity.this);
    }

    private class LoadDataBase extends AsyncTask<Context, Void, Void> {
        Context context;

        public LoadDataBase(Context context){
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Context... arg0) {
            // Create data base from assets folder.
            DatabaseHelper databaseHelper = new DatabaseHelper(arg0[0]);
            try {
                databaseHelper.createDataBase();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Closing the Data base.
            databaseHelper.close();

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            pauseSplashScreen(context);
        }
    }

    public void pauseSplashScreen(final Context context) {
        // New Thread call.
        new Thread() {
            // Running Thread.
            public void run() {
                int count = 0;
                while (count < 5) {
                    try {
                        Thread.sleep(1000);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                    count++;
                }

                Intent intent = new Intent(context, Activity2.class);
                startActivity(intent);
                finish();
            }
        }.start();
    }
}

The issue is : I am getting white screen at app start before splash screen and may be that is due to push notification registration given in above link.

What should i do to avoid that white screen. Please guide me.

mdDroid
  • 3,135
  • 2
  • 22
  • 34
Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95

2 Answers2

0

You are using a private inner class to create the database. And from there you call pauseSplashScreen(context). That then starts a new thread? Why?

I recommend giving the async task it's own class with a interface to give a signal back to your calling activity.

if you use

Intent intent = new Intent(context, Activity2.class);
startActivity(intent);

You don't have to call finish(), that call is better used to finish the current activity and get back to the previous one. (I get you use a thread to not block the rest of you app running, is it not better to let that thread get back to you, and then move to the next Activity?)

So, to answer your question, my guess is that the startup of your Activity takes some time, and what you see is a blanc layout loaded.

  • I removed whole database creation code and pauseSplash screen. Only kept the Push Notification registration code, then too the same thing happen... – Manoj Fegde May 26 '16 at 04:56
  • Might this be the issue? http://stackoverflow.com/questions/36575229/android-studio-2-0-pause-white-screen-on-app-first-run?rq=1 Otherwise: use the Debugger? or move the Push Notification registration code to the onResume()? – Flummox - don't be evil SE May 26 '16 at 06:16
0

It's just because you are using the debug version, when you change to the release version, it will be gone don't wry about it!

Ahmed I. Elsayed
  • 2,013
  • 2
  • 17
  • 30