What is the correct way to log in and register?

Asked

Viewed 588 times

0

I have two Activities, the main Myactivity and Loginactivity, how do I make the application detect logged in user when opening the application? And how to do for when the user presses the back button, the Activity Loginactivity does not return to the t

  • 3

    Your question is very wide. Try to provide more details, such as what you have been able to encode so far, for more accurate answers.

  • Patrick, I made the Login screen and a Register screen, but talking to some friends and they told me that I was doing wrong when calling the login screen direct in the main Activity, I wanted the application to detect if the user is logged in... I believe that only a few tips on which sequence of activities is good for me...

2 answers

1


One way to do it is like this:

public class AtividadeInicial extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        exibeTelaPrincipalOuSolicitaLogin();
        finish();
    }

    private void exibeTelaPrincipalOuSolicitaLogin() {
        Intent intent = new Intent(this, usuárioEstáLogado() ? AtividadePrincipal.class : AtividadeLogin.class);
        startActivity(intent);
    }

    private boolean usuárioEstáLogado() {
        // Sua implementação que retorna se o usuário está logado ou não,
        // obtida por exemplo verificando se o token de autenticação
        // guardado em SharedPreferences é diferente de null.
    }
}

If you preserve the authentication between calls to Web Services through a token with time to expire on the server, the login condition may no longer be valid if the user does not browse the application for long and the token expires. In that case it may be necessary to close all Activities open and request login again, which can be done by calling the method efetuarLogoffEVoltarParaATelaDeLogin() down below:

public abstract class AtividadeEmQueOUsuárioEstáLogado {
    protected void efetuarLogoffEVoltarParaATelaDeLogin() {
        invalidarTokenDeAutenticação();
        fecharTodasAsTelasEAbrirTelaDeLogin();
    }

    private void invalidarTokenDeAutenticação() {
        // Sua implementação que invalida o token atual
        // (por exemplo setando-o para null)
    }

    private void fecharTodasAsTelasEAbrirTelaDeLogin() {
        Intent intent = new Intent(this, AtividadeLogin.class); // ou AtividadeInicial.class
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}

public class AtividadePrincipal extends AtividadeEmQueOUsuárioEstáLogado {

    public métodoQualquer() {
        if (chameiUmWebServiceEConstateiQueOTokenExpirou) {
            efetuarLogoffEVoltarParaATelaDeLogin();
        }
    }
}

You may want to make the methods usuárioEstáLogado() and invalidarTokenDeAutenticação() globally accessible for use elsewhere, including in a conventional class or subclass of Application and eventually making them static (static).

  • Very nice Pio.. Thank you

  • @Israelsousa If you think this answer meets your need, mark it as accepted. If you have any questions just ask here in the comments.

  • @Israelsousa To accept the answer, click on the tag as shown in this figure: http://meta.pt.stackoverflow.com/a/1079/357

  • I marked Piovezan, that’s why I’m learning to use the OS now.

  • @Isrealsousa Fine, learning is part of it. But what answer did you want to mark? Because it was marked which is not mine. : ) You can change if you want.

  • It is that I am the application and could not identify. Kkk I will try again.

  • Yours is the first? I didn’t find the name only author or I don’t understand.

  • @Israelsousa Mine starts with "A way to do it is like this:" and at the end of it says it was answered by me (look for my avatar, an abstract purple drawing). The other, which is short, starts with "Dude, you can save the user in the sqlite database." and at the end it says that it was answered by the Eonardo (avatar with his face). To the left of the two answers, near the beginning of each one, is the mark that you must click to accept it.

  • @Israelsousa Beauty, now gone. :)

  • I found already, is that I am using the application of Stack Exchange. I marked as accepted.

  • @Israelsousa Jóia. Welcome to the community!

Show 6 more comments

0

  • Leonardo, but I wanted it was kind of like this. The user hasn’t logged in yet, so now he logs in and closes the app by pressing the back button... I wonder how it does this so the user doesn’t need to log in again without him clicking out...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.