0

I am new to Android Developing and I'm experiencing some problem and I can't really tell from my point of view why the app sending me back to login and I checked the Codes line by line what is the problem. I just change my Home design and the code in my Home.java is still the same but when I run the application when i tap the MainActivity it sent me back to Login, but

when I change the intent Activity in Login > MainActivity it works fine

but if Login > Home > MainActivity then it always sent me back in Login. How come that user is getting null after changing the Activity from Home.

Home.java

if (mAuth.getCurrentUser() != null) {

        mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());

    }
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId())
    {
        case R.id.nav_users:
            Intent intent = new Intent(Home.this,MainActivity.class);
            startActivity(intent);

        case R.id.nav_logout:
            logout();
            break;
    }

    drawer.closeDrawer(GravityCompat.START);
    return true;

}

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();

    if(currentUser==null)
    {
        sendToStart();
    }
}

private void sendToStart()
{
    Intent startIntent = new Intent(Home.this, LoginActivity.class);
    startActivity(startIntent);
    finish();
}
  • 1
    That's happening because you forgot to add break statement in your switch statement like this case R.id.nav_users: Intent intent = new Intent(Home.this,MainActivity.class); startActivity(intent); break; – ShehrozEK Nov 03 '18 at 20:13
  • give the full code and just hide only if there are secret parts along with the layout file – Aishik kirtaniya Nov 03 '18 at 20:13
  • 1
    @Shehroz Oh thank you! I didn't notice that, it is because of my negligence. – Gabriel Apostol Nov 03 '18 at 20:24
  • 1
    You can also check **[this](https://stackoverflow.com/questions/50885891/one-time-login-in-app-firebaseauth)** out. – Alex Mamo Nov 04 '18 at 11:38

0 Answers0