我已经尝试了我在这个网站上看到的所有内容但仍然没有运气。我有我的MainActivity,它检查用户是否已登录。如果他们有它启动一个名为ActiveThreadFragment的片段,如果他们没有它启动登录屏幕。然后,用户输入他们的凭据并按下登录。它会加载一个进度框,然后再次显示登录屏幕,并显示“登录成功”。我究竟做错了什么?可能真的很明显,但请看看我的代码。
MainActivity
if (PreferenceConnector.readString(this, "authToken") == null) {
Intent intent = new Intent(this, FirstLoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
} else {
initUI();
}
if (PreferenceConnector.readString(this, "authToken") != null &&
PreferenceConnector.readString(this, "authToken").equalsIgnoreCase("skip")) {
SharedPreferences.Editor editor = PreferenceConnector.getEditor(this);
editor.clear();
editor.commit();
}
}
FirstLoginActivity
private void initUI() {
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog progressDialog = ProgressDialog.show(FirstLoginActivity.this,
"Please wait", "Checking credentials", true);
progressDialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
if (isDataValid()) {
sendCredentials();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Login Failed - Invalid Username and/or " +
"Password", Toast.LENGTH_LONG).show();
}
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressDialog.dismiss();
}
}).start();
}
});
mSkipText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PreferenceConnector.writeString(getApplicationContext(), "authToken", "skip");
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
AndroidManifest
<activity android:name=".activity.FirstLoginActivity"
android:label="@string/app_name"
android:launchMode = "singleInstance">
</activity>
提前致谢:D
答案 0 :(得分:0)
尝试使用SharedPreferences
我不太确定你从哪里获得这个PreferenceConnector
课程,但我怀疑它是否能正确保存。
<强>读强>
SharedPreferences myPrefs = getActivity().getPreferences(Context.MODE_PRIVATE);
String authToken = myPrefs.getString("authToken",null);
<强>编写强>
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("authToken", newToken);
editor.commit()