使用共享首选项和意图 - 设置和设置应用程序



我目前正在开发一个应用程序,该应用程序在第一次打开时显示设置界面,随后打开应用程序时,它会显示设置界面。我实现此目的的方法是,一旦我的应用程序打开,它就会检查共享首选项是否为空。如果共享首选项为 null,则表示这是第一次打开应用程序,它将在共享首选项中设置一个值。然后下次打开应用程序时,当选中共享首选项时,它不再为空,将转到设置界面。问题是,当我第一次运行它时,它只显示一个白屏,没有状态栏或任何东西。以下是我的应用程序的代码片段

private void saveSharedPref()
{
SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_FILE, 0);
SharedPreferences.Editor editor = preferences.edit();
//Save values from Edit Text Controls
editor.putString("firstTime", "1234");
editor.commit();
}
private void loadSharedPreferences()
{
SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES_FILE, 0);
//Null check in case file does not exist
if(preferences != null) {
String checkFirst = preferences.getString("firstTime", "");
if (!checkFirst.equalsIgnoreCase("1234"))
{
Intent sendToSetup = new Intent (this, Setup.class);
startActivity(sendToSetup);
}
else
{
saveSharedPref();
}
}
}

我在我的onCreateMethod中调用loadSharedPreferences()

public class Setup extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup_pg1);
}

这是用户首次打开应用时重定向应用的位置。

任何帮助将不胜感激。提前感谢! 附言对不起,如果我的英语不好

编辑: 这是安卓清单

<?xml version="1.0" encoding="utf-8"?>

<application
android:allowBackup="true"
android:icon="@drawable/icon1"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SafeSwipe"
android:label="SafeSwipe-UI v.2"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".MainActivity"
android:label=""
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Setup" />
</application>

setup_pg1

<TextView
android:text="Setup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp"
android:id="@+id/textView" />
<Button
android:text="Continue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_marginTop="71dp"
android:id="@+id/button7" />

安装文件

public class Setup extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup_pg1);
}

}

首先,if(preferences != null)永远不会为真,因为您总是会从getSharedPreferences()

我在我的onCreate方法中调用loadSharedPreferences()

不,你不要...这是你的代码

public class Setup extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup_pg1);
}
}

再试一次。但是使用布尔值而不是一些随机字符串。

public class MainActivity extends AppCompatActivity {
public static final String PREFS_NAME = "MyPrefsFile";
public static final String KEY_FIRST_TIME = "firstTime";
@Override
protected void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.activity_main);
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
boolean firstTime = prefs.getBoolean(KEY_FIRST_TIME, false);
if (!firstTime) {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(KEY_FIRST_TIME, true);
editor.commit();    
} else {
Intent sendToSetup = new Intent (this, Setup.class);
startActivity(sendToSetup);
finish();
}
}
}

试试这个: 而不是检查首选项 != null 试试这个。.

if (preferences.getString("firstTime","").equals("1234"))
{
Intent sendToSetup = new Intent (this, Setup.class);
startActivity(sendToSetup);
}
else
saveSharedPref();

您的if(preferences != null)在这里不起作用,因为首选项不为空。这只是一个空洞的偏好。您可以尝试以下代码:

private void loadSharedPreferences()
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String checkFirst = preferences.getString("firstTime", "");
if(!checkFirst.equals("")) {        
Intent sendToSetup = new Intent (this, Setup.class);
startActivity(sendToSetup);
}
else
{
saveSharedPref();
}
}

saveSharedPref()方法:

private void saveSharedPref()
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
//Save values from Edit Text Controls
preferences.edit().putString("firstTime", "1234").apply();
}

onCreate()安装活动调用loadSharedPreferences()

希望这有帮助。

我在这段代码中没有看到任何关键问题。但是我有一些假设,您在测试程序时已经调用了saveSharedPref()方法,并且firstTime的值已经存在。因为重新编译程序后不会删除共享首选项,所以以下语句:

if (!checkFirst.equalsIgnoreCase("1234"))

给出假。 检查这一点的最佳方法是loadSharedPreferences()方法中设置断点并进行验证。 如果是这样,请尝试删除共享首选项(请参阅答案 https://stackoverflow.com/a/3687333/7677939)

或者只是在变量中给出另一个文件名SHARED_PREFERENCES_FILE

也可能是您调用loadSharedPreferences()的问题,但我在您的来源中看不到它。

更新:请注意,您不应该在Setup活动中调用loadSharedPreferences(),而应该在主活动中调用它!

最新更新