Migrate existing Android PNG Splashscreen to Splashscreen API

34 views Asked by At

I'm trying to migrate my existing Splash activity to the new Splashsreen API. Currently I have two png files - one is an English splashscreen, the other is a French splashscreen.

If the app is launched for the first time after installation, control is passed to a "terms" activity that displays the license agreement for the user to accept before proceeding to the main activity. Below is the current code for the splash activity:


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;

public class SplashActivity extends AppCompatActivity {

    public static final String TAG = "SplashActivity";


    final int SPLASH_DISPLAY_LENGTH = 2000;
    private ActivitySplashBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySplashBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        if(!isTaskRoot() && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)) {
            finish();
            return;
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                SharedPreferences preferences = getSharedPreferences("PREF_AGREE", Context.MODE_PRIVATE);
                boolean agreed = preferences.getBoolean("PREF_AGREE", false);
                if (agreed) {
                    Intent intent = new Intent(SplashActivity.this, MapsActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    SplashActivity.this.startActivity(intent);
                    SplashActivity.this.finish();
                } else {
                    Intent intent = new Intent(SplashActivity.this, TermsActivity.class);
                    SplashActivity.this.startActivity(intent);
                    SplashActivity.this.finish();
                }
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}

I have modified the code as follows:

        import android.content.Intent;
        import android.content.SharedPreferences;
        import android.os.Bundle;
        import android.window.SplashScreen;
        import androidx.annotation.NonNull;
        import androidx.appcompat.app.AppCompatActivity;


public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Use the SplashScreen API to hide the status bar and navigation bar
        SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
        splashScreen.OnExitAnimationListener((@NonNull SplashScreen splashScreen1) -> {
            // This callback will be triggered when the splash screen exit animation is complete.
            navigateToNextScreen();
        });
    }

    private void navigateToNextScreen() {
        SharedPreferences preferences = getSharedPreferences("PREF_AGREE", Context.MODE_PRIVATE);
        boolean agreed = preferences.getBoolean("PREF_AGREE", false);

        Intent intent;
        if (agreed) {
            intent = new Intent(SplashActivity.this, MapsActivity.class);
        } else {
            intent = new Intent(SplashActivity.this, TermsActivity.class);
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }
}

But the line SplashScreen splashScreen = SplashScreen.installSplashScreen(this); produces an error defining splashscreen

0

There are 0 answers