skip to Main Content
import com.google.firebase.auth.FirebaseUser;

/**
 * Loading screen activity
 */
public class SplashActivity extends AppCompatActivity {

    private FirebaseUser currentUser;

    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if ( currentUser != null ) {
                Intent intent=new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                    Intent intent = new Intent(SplashActivity.this, StartActivity.class);
                    startActivity(intent);
                    finish();
                }
                }
        },3000);

    }
}

I did this code

if ( currentUser != null ) {

But the above code didn’t seem to work, it always run this StartActivity.class

Unless the currentUser function is not working

2

Answers


  1. You’ve forgotten to initialize your currentUser variable. From the Firebase documentation on getting the current user on Android:

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        // User is signed in
    } else {
        // No user is signed in
    }
    
    Login or Signup to reply.
  2. You need to initialize FirebaseAuth to get the credentials with FirebaseUser variable.

    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.auth.FirebaseUser;
    
    public class SplashActivity extends AppCompatActivity {
    
    private FirebaseUser currentUser;
    private FirebaseAuth mAuth = FirebaseAuth.getInstance();
    
    private Handler handler;
    
    /*
    * Checks if user is logged in & return true or false value
    */
    public boolean isUserLoggedIn() {
        return this.mAuth.getCurrentUser() != null;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        
        handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Use the value to fire login or start activity
                if (isUserLoggedIn()) {
                    /*
                    * User is logged in (Start MainActivity)
                    */
                    
                } else {
                    /*
                    * User doesn't exist (Log in / Create Account if needed)
                    */
                }
            }
        },3000);
        
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search