如何设置仅在 android 中首次启动时出现的 activity?

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

我一直在尝试实现一个 activity,它只会在应用程序首次启动时出现。为此,我创建了以下 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);
    }
}

它的最后一个方法 isFirstTime 正在返回不允许执行的空值。

这是我的启动器 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);
    }
}

注释代码调用 LaunchManager class。会不会是我在其他活动中使用的ViewBinding 没有在Launch Manager 中指定的,如果是的话如何实现?

请帮我解决这个问题。

当您创建共享首选项助手的新实例时 class 您需要对其进行初始化。您从未对其进行初始化,因此返回的值将始终为空。您需要使用上下文调用它(请参阅“LaunchManager”class 中的构造函数)。

编辑:为了澄清,您需要在 onCreate 方法中调用它并将上下文传递给它。

您没有初始化 LaunchManager class...首先初始化它:

LaunchManager launchmanager = new LaunchManager 
(SplashScreenActivity.this);

然后在设置共享首选项后 class

if(launchManager.isFirstTime()) {
                    launchManager.setFirstLaunch(false);
                    startActivity(new
Intent(getApplicationContext(), 
SliderScreenActivity.class));
                }
                /**else {
                    startActivity(new 

Intent(getApplicationContext(),MainActivity.class));***/ }

现在覆盖 onStart() 方法并检查

if (!launchManager.isFirstTime()){
    startActivity(new 
Intent(getApplicationContext(),MainActivity.class));
finish();
}

首先让你的LaunchManagerclass成为单例class。

    object LaunchManager {
        val PREF_NAME = "LaunchManger";
        val IS_FIRST_TIME = "isFirst";
    
      fun setFirstLaunch(context: Contex, isFirstTime: Boolean) {
      val sharedPreferences = context.getSharedPreferences(PREF_NAME, MODE_PRIVATE)
            val editor = sharedPreferences.edit();
            editor.putString(IS_FIRST_TIME, isFirstTime)
            editor.apply()  
       }
    
       fun getFirstLaunch(context: Context) {
       val sharedPreferences = context.getSharedPreferences(PREF_NAME, MODE_PRIVATE)
            return sharedPreferences.getBoolean(IS_FIRST_TIME, true)
       }
}

现在在你的 SplashScreenActivity

Handler(Looper.myLooper()!!).postDelayed({
     if(LaunchManager.getFirstLaunch(this)){
         LaunchManager.setFirstLaunch(this, false)
         startActivity(new Intent(getApplicationContext(), SliderScreenActivity.class));
     }
     else{
      startActivity(new Intent(getApplicationContext(),MainActivity.class));
     }
},1500)

注意:我用 Kotlin 写了答案,但它可以被 Android Studio 自动转换成 Java。
在这里使用 Singleton class 是一个更好的选择,因为你只想对你的 sharedPrefs 使用一次 class。
让我知道这是否解决了您的查询