我正在尝试传递一个意图来检查会话名称,如果它匹配“Admin”,那么我想要一个按钮变得可见。
当调用toast时,会话名称是完全空白的
我已经从Login类中发送了这样的意图:
public void onClick(View v) {
final String username = usernameET.getText().toString();
final String password = passwordET.getText().toString();
boolean success = db.Login(username, password);
boolean adminSuccess = db.adminLogin(username, password);
if(success)
{
Log.d("login", "user logged");
session = true;
Intent i = new Intent(Login.this, MainActivity.class);
Intent a = new Intent(Login.this, MyAccount.class);
Intent h = new Intent(Login.this, Homepage.class);
i.putExtra("loginSuccess", session);
i.putExtra("sessionName", username);
a.putExtra("sessionName", username);
h.putExtra("sessionName", username);
startActivity(a);
startActivity(i);
startActivity(h);
}
else if (adminSuccess)
{
Log.d("login", "admin logged");
session = true;
Intent i = new Intent(Login.this, MainActivity.class);
Intent a = new Intent(Login.this, MyAccount.class);
Intent h = new Intent(Login.this, Homepage.class);
i.putExtra("loginSuccess", session);
h.putExtra("loginSuccess", session);
i.putExtra("sessionName", username);
a.putExtra("sessionName", username);
h.putExtra("sessionName", username);
startActivity(a);
startActivity(h);
startActivity(i);
}
else
{
Log.d("login", "user not logged");
}
}
然后我在 MainActivity.java (有效)和我的 Homepage.java (没有)
中调用它在主页的 onCreate()方法中如此:
//Check if admin is logged in for editting
final Intent intent = getIntent();
boolean loginSuccess = intent.getBooleanExtra("loginSuccess", true);
String sessionName = intent.getStringExtra("sessionName");
Toast.makeText(Homepage.this, sessionName, Toast.LENGTH_LONG).show();
if (sessionName == "Admin")
{
editBtn.setVisibility(View.VISIBLE);
}
}
它在我的MainActivity中以相同的方式工作
我打算在 onResume()方法中执行此操作吗?不太确定为什么不通过