第一次应用程序启动介绍错误(viewpager)
first time app launch intro error (viewpager)
我试图添加一些仅在应用首次启动时 运行 才会显示的介绍屏幕,之后它将直接加载登录页面
我使用以下指南来实现此目的
SharedPreferences sp = getSharedPreferences(MyPrefs, 0);
if (sp.getBoolean("first", true)) {
SharedPreferences.Editor editor = sp.edit();
/*editor.putBoolean("first", false);*/
sp.edit().putBoolean("first", false).commit();
editor.commit();
Intent intent = new Intent(this, login.class); //call your ViewPager class
startActivity(intent);
}
但应用程序在第一次使用应用程序时会跳过介绍部分并加载登录页面,并在再次启动时加载介绍页面
我怎么能扭转这个
谢谢
您的情况有问题。试试这个代码:
SharedPreferences sp = getSharedPreferences(MyPrefs, 0);
if (sp.getBoolean("first", true)) {
sp.edit().putBoolean("first", false).apply();
// Show Intro Activity
} else {
Intent intent = new Intent(this, login.class);
startActivity(intent);
}
先确认一下是不是第一次。如果是,则显示简介 Activity
。如果不是第一次,则显示登录 Activity
.
注意我删除了一些 SharedPreferences 代码,其中一部分是多余的。我也把 commit()
改成 apply()
.
SharedPreferences sp = getSharedPreferences(MyPrefs, 0);
if(sp.contains("first"){
Intent intent = new Intent(this, login.class); //call your ViewPager class
startActivity(intent);
}
else{
SharedPreferences.Editor editor = sp.edit();
sp.edit().putBoolean("first", true).commit();
editor.commit();
}
解释:
当您第一次启动您的应用程序时,变量 "first" 不会出现在共享首选项中 :) 因为您 getBoolean
将默认值指定为 return 以防 "first" 不存在发现是真的:)你所有的代码都是为了折腾:)
正确的做法:)
首先检查 "first" 关键字是否存在??如果不是,这意味着您是第一次启动它,请显示介绍屏幕 :) 但不要忘记先输入值为 true 的密钥 :)
所以下次启动时,第一个密钥将出现在共享首选项中所以现在您知道您正在第二次启动 :) 您可以跳过介绍并启动登录 activity :)
不要为 "first" 和所有的值而烦恼 :) 您只需要知道这个键是否存在就够了 :)
我试图添加一些仅在应用首次启动时 运行 才会显示的介绍屏幕,之后它将直接加载登录页面 我使用以下指南来实现此目的
SharedPreferences sp = getSharedPreferences(MyPrefs, 0);
if (sp.getBoolean("first", true)) {
SharedPreferences.Editor editor = sp.edit();
/*editor.putBoolean("first", false);*/
sp.edit().putBoolean("first", false).commit();
editor.commit();
Intent intent = new Intent(this, login.class); //call your ViewPager class
startActivity(intent);
}
但应用程序在第一次使用应用程序时会跳过介绍部分并加载登录页面,并在再次启动时加载介绍页面 我怎么能扭转这个 谢谢
您的情况有问题。试试这个代码:
SharedPreferences sp = getSharedPreferences(MyPrefs, 0);
if (sp.getBoolean("first", true)) {
sp.edit().putBoolean("first", false).apply();
// Show Intro Activity
} else {
Intent intent = new Intent(this, login.class);
startActivity(intent);
}
先确认一下是不是第一次。如果是,则显示简介 Activity
。如果不是第一次,则显示登录 Activity
.
注意我删除了一些 SharedPreferences 代码,其中一部分是多余的。我也把 commit()
改成 apply()
.
SharedPreferences sp = getSharedPreferences(MyPrefs, 0);
if(sp.contains("first"){
Intent intent = new Intent(this, login.class); //call your ViewPager class
startActivity(intent);
}
else{
SharedPreferences.Editor editor = sp.edit();
sp.edit().putBoolean("first", true).commit();
editor.commit();
}
解释:
当您第一次启动您的应用程序时,变量 "first" 不会出现在共享首选项中 :) 因为您 getBoolean
将默认值指定为 return 以防 "first" 不存在发现是真的:)你所有的代码都是为了折腾:)
正确的做法:) 首先检查 "first" 关键字是否存在??如果不是,这意味着您是第一次启动它,请显示介绍屏幕 :) 但不要忘记先输入值为 true 的密钥 :)
所以下次启动时,第一个密钥将出现在共享首选项中所以现在您知道您正在第二次启动 :) 您可以跳过介绍并启动登录 activity :)
不要为 "first" 和所有的值而烦恼 :) 您只需要知道这个键是否存在就够了 :)