Search code examples
javaandroidandroid-activitysplash-screen

How to set up an activity that only appears on first launch in android?


I have been trying to implement an activity which will only appear on the first launch of the application. For this I have created the following LaunchManager class:

package com.example.mylist;

import android.content.Context;
import android.content.SharedPreferences;

//Class to manage launching activities
//(to make the slider appear only on first launch)
public class LaunchManager {
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    private static String PREF_NAME = "LaunchManger";
    private static String IS_FIRST_TIME = "isFirst";

    public LaunchManager(Context context) {
        sharedPreferences = context.getSharedPreferences(PREF_NAME, 0);
        editor = sharedPreferences.edit();
    }

    public void setFirstLaunch(boolean isFirst) {
        editor.putBoolean(IS_FIRST_TIME, isFirst);
        editor.commit();
    }

    public boolean isFirstTime() {
        return sharedPreferences.getBoolean(IS_FIRST_TIME, true);
    }
}

Its last method isFirstTime is returning null value which isn't allowing the execution.

Here's my launcher activity:

package com.example.mylist;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;

import com.example.mylist.databinding.ActivityMainBinding;
import com.example.mylist.databinding.ActivitySplashScreenBinding;

public class SplashScreenActivity extends AppCompatActivity {
    ActivitySplashScreenBinding binding;
    LaunchManager launchManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //view binding
        binding = ActivitySplashScreenBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        //animated background
        AnimationDrawable animationDrawable =
                (AnimationDrawable) binding.rlSplashScreen.getBackground();
        animationDrawable.setEnterFadeDuration(2000);
        animationDrawable.setExitFadeDuration(4000);
        animationDrawable.start();

        //setting time and intents
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                /* if(launchManager.isFirstTime()) {
                    launchManager.setFirstLaunch(false);
                    startActivity(new Intent(getApplicationContext(),
                                             SliderScreenActivity.class));
                }
                else { */
                    startActivity(new Intent(getApplicationContext(),
                                             MainActivity.class));
                //}
            }
        }, 1500);
    }
}

The commented code calls the LaunchManager class. Could it be something to do with the ViewBinding I have utilised in other activities and haven't specified in the Launch Manager, if it is then how to implement that?

Kindly help me resolve this issue.


Solution

  • When you create a new instance of your shared preferences helper class you need to initialize it. You've never initialized it, hence, the values returned will always be null. You need to call it with the context (See the constructor in the "LaunchManager" class).

    Edit: To clarify, you need to call it in the onCreate method and pass the context to it.