【问题标题】:Setting Up a Welcome Screen in Android [duplicate]在 Android 中设置欢迎屏幕 [重复]
【发布时间】:2015-05-18 05:57:11
【问题描述】:

我正在尝试创建一个欢迎屏幕。我的问题是,每次我重新打开应用程序时,它都会再次运行 onCreate 函数(下面的代码)并重新初始化变量,就好像它是第一次一样。我尝试使用具有共享首选项的计数器但得到相同结果。我的想法是我的应用程序将在第一次加载时通过 Oncreate 方法运行,然后创建一个名为 firstBoot = true 的布尔值。然后将其更改为 false,但是如果布尔值不存在,我如何测试它是否为 false然而?任何帮助将不胜感激。

(在 onCreate 方法中)

 final String PREFS_NAME = "MyPrefsFile";

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

if (settings.getBoolean("my_first_time", true)) {
    //the app is being launched for first time, do something
    Log.d("Comments", "First time");

    // first time task
    setContentView(R.layout.setup);

    // record the fact that the app has been started at least once
    settings.edit().putBoolean("my_first_time", false).commit();
}
else
{
    setContentView(R.layout.test);
}

【问题讨论】:

标签: java android android-activity boolean sharedpreferences


【解决方案1】:

此代码将帮助您获得所需的内容

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();

     int count = prefs.getInt("count", 0);
    count++;
            editor.putInt("count", count);
     editor.commit();

    if(count==1)
    { welcome screen code
    }
    else
    { your code
    }

【讨论】:

  • 你不能使用 getInt();在这个情况下。我得到一个找不到符号方法的错误
  • 检查编辑的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 2020-05-19
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
相关资源
最近更新 更多