KMarto
KMarto

Reputation: 300

Logging In for android app

I am creating an android app that requires the user to log in after starting the main activity. So I store the status in the Application and update it in the Login Activity.

public class AppActivity extends Application {
    ........
    public static boolean isLogged =false;
    ......
}

In the main, I check the status, if the user is logged in I show the main activity if not I show the login activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(!AppActivity.isLogged){
        Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
        startActivity(intent);
    }
}

Also I check onResume if the user is logged in

From there I update the Login Activity like so

public class LoginActivity extends AppCompatActivity {
    btnLogin.setOnlickListener(){

        @Override
        public void onClick(View view) {
            AppActivity.isLogged = true;
            finish();
        }
    }
}

The problem is,I have to log in twice for the app to redirect me to the main activity

Upvotes: 0

Views: 66

Answers (1)

WoogieNoogie
WoogieNoogie

Reputation: 1258

finish() is not sending you back to the activity via onCreate(), it's simply returning you to the activity. It will, however, send you through that activity's onResume(). If you place your login check in onResume instead of onCreate, it will work. There are also other methods you could look at, but that would be the simplest for you to implement with the existing code.

Upvotes: 1

Related Questions