Define different Android home screens

3

This question I already asked for swift

I'm doing an app in Android , in which I check if a user is already registered or not.

If you are registered, I show you a screen, but if you are not registered I show you another one.

How can I do to show one screen or another?

Here is my code:

if (comprobarUsuario(String datos)) {
   //Existe el usuario, por lo que se va directamente a la app
}
else {
   //No existe el usuario, por lo que se muestra el formulario
}
    
asked by Comunidad 02.09.2016 в 12:54
source

2 answers

1

If they are Activity your screens simply perform the intent to open them:

    Intent intent;
    if (comprobarUsuario(String datos)) {
        //Existe el usuario, por lo que se va directamente a la app
        intent = new Intent(this, MainActivity.class);
    }else {
        //No existe el usuario, por lo que se muestra el formulario
        intent = new Intent(this, LoginActivity.class);
    }
    //Inicia la Activity.
    startActivity(intent);

If the "screens" were Fragments it would be done by a transaction replacing the fragment:

    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    if (comprobarUsuario(String datos)) {
        //Existe el usuario, por lo que se va directamente a la app
        Fragment fragment = new FragmentMain();
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction =
                fragmentManager.beginTransaction();
        fragmentTransaction.replace(android.R.id.content, fragment);
        fragmentTransaction.commit();
    }   else {
        //No existe el usuario, por lo que se muestra el formulario
        Fragment fragment = new FragmentLogin();
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction =
                fragmentManager.beginTransaction();
        fragmentTransaction.replace(android.R.id.content, fragment);
        fragmentTransaction.commit();
    }
    
answered by 02.09.2016 / 16:11
source
2

Create two Activity s and launch them according to what you need:

Intent intent = null;

if (comprobarUsuario(String datos)) {
    intent = new Intent(this, AplicacionActivity.class);
} else {
    intent = new Intent(this, RegistrarUsuarioActivity.class);
}
startActivity(intent);
    
answered by 02.09.2016 в 14:04