0

我一直在尝试实现一个只会在应用程序首次启动时出现的活动。为此,我创建了以下 LaunchManager 类:

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 返回不允许执行的空值。

这是我的启动器活动:

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 类。这可能与我在其他活动中使用的 ViewBinding 有关,并且没有在启动管理器中指定,如果是那么如何实现呢?

请帮我解决这个问题。

4

3 回答 3

3

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

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

于 2021-06-20T03:50:20.670 回答
1

你没有初始化你的 LaunchManager 类...首先初始化它:

LaunchManager launchmanager = new LaunchManager 
(SplashScreenActivity.this);

然后在设置你共享的偏好类之后

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

意图(getApplicationContext(),MainActivity.class));***/}

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

if (!launchManager.isFirstTime()){
    startActivity(new 
Intent(getApplicationContext(),MainActivity.class));
finish();
}
于 2021-06-20T04:55:49.757 回答
1

首先使您的LaunchManager班级成为单身班级。

    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 类是一个更好的选择,因为您只想对 sharedPrefs 使用这个类一次。
让我知道这是否能解决您的问题

于 2021-06-20T06:37:50.747 回答