Anubhav Jain
Anubhav Jain

Reputation: 13

Android: Using the app home page as launcher activity

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

Answers (2)

Aakash
Aakash

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

Mariano Zorrilla
Mariano Zorrilla

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

Related Questions