我认为我可以在登录后绕过登录活动但是如何检索并将用户名传递给下一个活动。我得到的用户名是NULL
这是登录活动
https://graph.microsoft.com/{version}/users/UPN/photo
这是我的NextActivity,其中包含菜单栏中的注销操作
public class LoginActivity extends AppCompatActivity {
/*
* Database helper field
*/
private DatabaseHelper databaseHelper;
/*
Edittext username and password
*/
private EditText uName, pwd;
/*
Button login and signup
*/
private Button loginButton, signupButton;
private String username, password;
/**
* Create activity when register button and log in button clicked
* @param savedInstanceState saved instance state
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar)findViewById(R.id.login_toolbar);
toolbar.setTitle("LOGIN");
databaseHelper = new DatabaseHelper(this);
uName = (EditText)findViewById(R.id.uNameToLogin_editText);
pwd = (EditText)findViewById(R.id.pwdToLogin_editText);
SharedPreferences sharedPreferences = getSharedPreferences
(getString(R.string.SHARED_PREFS), MODE_PRIVATE);
boolean isLoggedIn = sharedPreferences.getBoolean(getString(R.string.LOGGEDIN), false);
if (isLoggedIn){
Intent newIntent = new Intent(LoginActivity.this, FinderActivity.class);
newIntent.putExtra("Username", username);
startActivity(newIntent);
finish();
}
/*
* Sign up button clicked takes user to registration form
*/
signupButton = (Button)findViewById(R.id.signupButton);
signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
/*
*Log in activity
*/
loginButton = (Button)findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username = uName.getText().toString();
password = pwd.getText().toString();
//log in successfull using correct username and password
String pass = databaseHelper.searchPassword(username);
if (pass.equals(password)){
SharedPreferences sharedPreferences = getSharedPreferences
(getString(R.string.SHARED_PREFS), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(getString(R.string.LOGGEDIN), true);
editor.commit();
Toast.makeText(LoginActivity.this, "Welcome! " + username,
Toast.LENGTH_LONG).show();
//change to new search intent
Intent newIntent = new Intent(LoginActivity.this, FinderActivity.class);
newIntent.putExtra("Username", username);
startActivity(newIntent);
finish();
} else {
//error shows if inccorrect password or username
Toast.makeText(LoginActivity.this, "Username or Password Incorrect!, Try Again",
Toast.LENGTH_LONG).show();
}
}
});
}
}
答案 0 :(得分:1)
如果你想用意图传递数据,你应该在FinderActivity.java中的onCreate上设置它
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
if (getIntent() != null) {
final Bundle extras = getIntent().getExtras();
if (extras != null) {
userName = extras.getString("Username");
}
}
}
如果你想保存在SharedPreferences上,你必须保存在LoginActivity上并获得FinderActivity.java 一个解释如何 - >的链接How to use SharedPreferences in Android to store, fetch and edit values