SplashScreen api with different opener activity

Viewed 39

I just now started using the SplashScreen api. I have removed my old SplashActivity and I am calling installSplashScreen() within MainActivity before setContentView.

The problem is when I was using SplashActivity (the old method), I had the chance to make a choice to open the next activity. However, with this new SplashScreen api, I couldn't think of any ways to have the same thing. I am already inside the MainActivity when the app is open.

The thing is I don't always open MainActivity after Splash, I have another activity too that needs to be opened first occasionally right after Splash.

How could I do that? Could someone point me in the right direction?

1 Answers

I found this guide on the Android Developer site: Keep the splash screen on-screen for longer periods

This can be used to check for the condition to load another activity and then load that activity rather than the MainActivity

What you can do is, in the ViewTreeObserver.OnPreDrawListener's onPreDraw method, you can implement it something like this:

@Override
public boolean onPreDraw() {
    // Check if other activity than MainActivity is to be loaded:
    boolean shouldLoadOtherActivity = /* some condition */;
    if (shouldLoadOtherActivity) {
        // Create intent here to load OtherActivity
        Intent intent = new Intent(this, OtherActivity.class);
        startActivity(intent);

        finish(); // finish MainActivity
    }
    content.getViewTreeObserver().removeOnPreDrawListener(this); // remove the listener
    return true;
}
Related