Search code examples
androidandroid-studioandroid-handlersplash-screenandroid-thread

Splash screen determining, which Activity to load on startup?


I am making a Splash screen, which determines which Activity to load, based on whether the App is being started for the first time (or not)..

The code is running in it's own Activity - MainActivity, which will act as the Splash screen. If it's the first time starting, I load IntroActivity.. If it's been started before, I load PrimaryActivity.

I have a couple of Questions:

1) - Is using runOnUiThread the correct way to do this?

2) - I have researched topics relating to Splash screens here on StackOverflow, which suggest the use of a Handler - is this recommended in my specific use case?

3) - Should I be closing this Thread once I determine which Activity to load, and if so, how should I go about doing this?

Bonus:

4) - I intend to eventually make this Activity be a pop-up style loading window..

What is the simplest way to achieve this?

Thanks in advance for any help provided!


My current code is shown below:

public class MainActivity extends AppCompatActivity {

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

    //  Make a Toast pop-up.
    Toast.makeText(MainActivity.this, "Checking Settings...", Toast.LENGTH_LONG).show();


    ////  BEGIN PREFERENCES CHECK  ////

    //  Set the wait time for the Splash screen.
    final int SPLASH_WAIT_TIME = 5000;

    //  Start new Thread to check for first start and load appropriate Activity.
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {

                //  Wait before continuing.
                try {
                    Thread.sleep(SPLASH_WAIT_TIME);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            //  Initialize SharedPreferences.
            SharedPreferences getPrefs = PreferenceManager
                    .getDefaultSharedPreferences(getBaseContext());

            //  Create a new boolean and preference and set it to true.
            boolean isFirstStart = getPrefs.getBoolean("firstStart", true);

            //  If the App has NEVER started before...
            if (isFirstStart) {

                //  Declare an Intent for loading IntroActivity.
                final Intent intentLoadIntro = new Intent(MainActivity.this, IntroActivity.class);

                //  Launch IntroActivity.
                runOnUiThread(new Runnable() {
                    @Override public void run() {
                        startActivity(intentLoadIntro);
                    }
                });

                //  Make a new Preferences Editor.
                SharedPreferences.Editor prefsEditor = getPrefs.edit();
                //  Edit Preference to make firstStart False so Intro never loads again.
                prefsEditor.putBoolean("firstStart", false);
                //  Apply the changes.
                prefsEditor.apply();

                //  Close MainActivity so the Back hardware button doesn't return to it.
                finish();

            }

            //  If the App HAS been started before...
            else {

                //  Declare an Intent for loading PrimaryActivity.
                final Intent intentLoadPrimary = new Intent (MainActivity.this, PrimaryActivity.class);

                //  Launch PrimaryActivity.
                runOnUiThread(new Runnable() {
                    @Override public void run() {
                        startActivity(intentLoadPrimary);
                    }
                });

                //  Close MainActivity so the Back hardware button doesn't return to it.
                finish();

            }

        }
    });

    //  Start Thread t to determine Activity to load after Splash (MainActivity).
    t.start();

//  END of onCreate.
}

//  End of MainActivity.
}

Solution

  • This is the best way to do it. Get the shared pref to see if its the users first time. If it is, take them to the first time activity, other wise, take them to main activity.

    If the user deleted the app and reinstalled, they are considered first time users again since this information is stored on the local device. If you want this user based, implement a database to store these tags by the user id. However, the flow would be similar.

    In your onCreate of your Splash activity

    //  Initialize SharedPreferences.
    SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    
    //  Create a new boolean and preference and set it to true.
    boolean isFirstStart = getPrefs.getBoolean("firstStart", true);    
    
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            if (isFirstTime) { //first time user is here.
                Intent intent = new Intent(Splash.this, FirstTime.class);
                startActivity(intent);
                finish();
            } else { //user has been here before.
                Intent intent = new Intent(Splash.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }, 500);   //half second
    

    In your first FirstTime activity, once the user is done doing whatever you want them to do, you then will update your shared pref and move them BACK to the splash screen to verify.

    //  Make a new Preferences Editor.
    SharedPreferences.Editor prefsEditor = getPrefs.edit();
    //  Edit Preference to make firstStart False so Intro never loads again.
    prefsEditor.putBoolean("firstStart", false);
    //  Apply the changes.
    prefsEditor.apply();
    // Go back to Splash...
    Intent intent = new Intent(FirstTime.this, Splash.class);
    startActivity(intent);
    finish();