'Splash Animation shows only once in Android

The splash screen's supposed to work every time I use my app But once I clear the application from task manager and restart it.

The animation doesn't appear and it goes straight to the login screen with no animation occurring.

package com.example.promilek;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

@SuppressLint("CustomSplashScreen")
public class SplashScreen extends AppCompatActivity {

    private static final int SPLASH_SCREEN = 2000;

    //Variables
    Animation topAnim;
    ImageView ImageViewButla;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow() .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash_screen);

        topAnim= AnimationUtils.loadAnimation(this, R.anim.top_animation);

        ImageViewButla = findViewById(R.id.imageViewButla);

        ImageViewButla.setAnimation(topAnim);

        new Handler() .postDelayed(() -> {
            Intent intent = new Intent(SplashScreen.this, Login.class);
            startActivity(intent);
            
            finish();
        },SPLASH_SCREEN);
    }
}


Solution 1:[1]

In my solution, I will use the Thread class to do this. At least this will give you more control in the activity lifecycle methods.

I have also included a SessionManager to help you incase you need it now or later.

Activity with animation

@SuppressLint("CustomSplashScreen")
public class SplashScreen extends AppCompatActivity {

    private static final int SPLASH_SCREEN = 2000;


    //Variables
    Animation topAnim;
    ImageView ImageViewButla;
    private Thread counterThread;

    Context mContext;
    private SessionManager sessionManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow() .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash_screen);


        mContext = getApplicationContext(); // Get application context


        topAnim = AnimationUtils.loadAnimation(this, R.anim.top_animation);
        ImageViewButla = findViewById(R.id.imageViewButla);
        ImageViewButla.setAnimation(topAnim);

        sessionManager = new SessionManager(mContext); // Initialize Session manager object


        // Counter thread
        counterThread = new Thread() {
            @Override
            public void run() {

                try {

                    int wait = 0; // Thread wait time

                    // Loop
                    while (wait < SPLASH_SCREEN) {

                        sleep(100); // Sleep
                        wait += 100;
                    }

                    Intent intent;
                    if (sessionManager.isSignedIn()) {

                        // Launch MainActivity
                        intent = new Intent(SplashActivity.this, MainActivity.class);

                    } else {

                        // Launch Signin or SignUp activity
                        intent = new Intent(SplashActivity.this,
                            SignInSignUpActivity.class);
                    }

                    startActivity(intent); // Start activity
                    finish(); // Exit Activity

                } catch (Exception ignored) {
                } finally {

                    finish(); // Exit Activity
                }
            }
        };
    }
}


    @Override
    public void onBackPressed() {
        super.onBackPressed(); // Exit
        counterThread.start(); // Start thread
    }

    @Override
    public void onResume() {
        super.onResume();

        counterThread.start(); // Start thread
    }

    @Override
    protected void onStop() {
        super.onStop();

        counterThread.interrupt(); // Interrupt thread on activity exit
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        counterThread.interrupt(); // Interrupt thread on activity exit
}

SessionManager class

Below is the code for the session manager

public class SessionManager {

    private static final String KEY_IS_SIGNED_IN = "isSignedIn";
    private static String PREFERENCE_NAME; // Shared Preference File Name

    // Shared Preference
    private final SharedPreferences sharedPreferences;
    private final SharedPreferences.Editor editor;

    // Shared Preference mode
    int PRIVATE_MODE = 0;

    @SuppressLint("CommitPrefEdits")
    public SessionManager(Context context) {

       this.sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, PRIVATE_MODE);
        this.editor = sharedPreferences.edit();
        PREFERENCE_NAME = context.getResources().getString(R.string.app_name) +
            "_SessionManagerPreference";
    }

    public boolean isSignedIn() {
        return sharedPreferences.getBoolean(KEY_IS_SIGNED_IN, false);
    }

    public void setSignedIn(boolean isLoggedIn) {
        editor.putBoolean(KEY_IS_SIGNED_IN, isLoggedIn);
        editor.commit(); // Commit Changes
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Kariuki