Issue
I have a question that whatever I search i can't find the answer for. So I have 3 Activities:StartActivity, LoginActivity and MainActivity In StartActivity I have 2 buttons: -Login - it sends me to LoginActivity -Continue as guest - it sends me to MainActivity In Login activity i have:
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if(loginUser(false)) {
String username = String.valueOf(userName);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("USERNAME", username);
startActivity(intent);
finish();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
and in MainActivity I have :
private String username = getIntent().getStringExtra("USERNAME");
But when I try to login as guest the String username has nothing to get and gives me error. So how can I make String username getIntent only when I open the activity from LoginActivity and don't do anything when I open it from ContinueAsGuest button in StartActivity?
Solution
button in StartActivity only take you as guest right?if so code will look like this
guestBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String username = String.valueOf(userName);
Intent intent = new Intent(StartActivity.this, MainActivity.class);
intent.putExtra("USERNAME", username);
startActivity(intent);
finish();
}
} catch (SQLException e) {
e.printStackTrace();
}
});
Answered By - androidLearner
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.