【问题标题】:How to set up an activity that only appears on first launch in android?如何设置仅在 android 中首次启动时出现的活动?
【发布时间】:2021-09-04 05:00:27
【问题描述】:

我一直在尝试实现一个只会在应用程序首次启动时出现的活动。为此,我创建了以下 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 有关,并且没有在 Launch Manager 中指定,如果是那么如何实现?

请帮我解决这个问题。

【问题讨论】:

  • 您可以在这里将LaunchManager 类设为单例类

标签: java android android-activity splash-screen


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    LaunchManager launchmanager = new LaunchManager 
    (SplashScreenActivity.this);
    

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

    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();
    }
    

    【讨论】:

      【解决方案3】:

      首先将您的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 使用这个类一次。
      让我知道这是否能解决您的问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-30
        • 1970-01-01
        • 2021-06-06
        • 1970-01-01
        • 1970-01-01
        • 2021-05-06
        • 1970-01-01
        相关资源
        最近更新 更多