Reputation: 13
I am building a simple facebook login application in android. I am using my login activity as the launcher activity. However, I would like to use my home page as launcher activity if the user is already logged in, otherwise login activity act as the launcher activity. Can anyone tell me the right way to do that?
Upvotes: 0
Views: 68
Reputation: 5251
When logging in first time do this:
SharedPreferences shared;
shared=getSharedPreferences("com.example", Context.MODE_PRIVATE);
shared.edit().putBoolean("loggedin",true).apply();
After you are already logged in
in Your onCreate() of loginActivity do this:
SharedPreferences shared;
shared=getSharedPreferences("com.example", Context.MODE_PRIVATE);
if(shared.getBoolean("loggedin",false)){
startActivity(new Intent(LoginActivity.this,Homepage.class));
finish();
}
Upvotes: 0
Reputation: 7666
If the user login, save that data, could be a string value, int, boolean, etc... check the data and if matches, make a staractivity intent.
Use my library to achieve that: KeySaver
In your Login Button (onClickListener) make this:
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!KeySaver.isExist(this, "haslogin")){
KeySaver.saveShare(this, "haslogin", true);
}
});
And at the very beginning of your Login Activity make this:
if(KeySaver.isExist(this, "haslogin")){
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(myIntent);
}else{
// do your code for login, etc
}
Upvotes: 1