Using Progressidialog when Logging in

Asked

Viewed 306 times

1

I am trying to insert a Progressdialog in the login screen of my Android APP.

In parts it is working, only when the login is successfully done, but when something is wrong as password for example, the Progressdialog does not close.

Code:

package com.parse.starter.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.starter.R;

public class LoginActivity extends AppCompatActivity {


    private EditText editLoginUsuario;
    private EditText editLoginSenha;
    private Button botaoLogar;
    private ProgressDialog progressDialog;

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


        editLoginUsuario = (EditText) findViewById(R.id.edit_login_usuario);
        editLoginSenha = (EditText) findViewById(R.id.edit_login_senha);
        botaoLogar = (Button) findViewById(R.id.button_logar);

        //Verificar se o usuario esta logado
        verificarUsuarioLogado();

         //configuração PROGRESSDIALOG
        //Configura barra de progresso passando o contexto
        progressDialog = new ProgressDialog(LoginActivity.this );

        //Configura o título da progress dialog
        //progressDialog.setTitle("Titulo da barra");

        //configura a mensagem de que está sendo feito o carregamento
        progressDialog.setMessage("Efetuando o login!!!");

        //configura se a progressDialog pode ser cancelada pelo usuário
        progressDialog.setCancelable(false);

        //Adicionar evento de click no botao logar
        botaoLogar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Exibe a barra no momento que é iniciado o processo
                progressDialog.show();


                String usuario = editLoginUsuario.getText().toString();
                String senha = editLoginSenha.getText().toString();

                verificarLogin( usuario, senha );
            }
        });


    }

    private void verificarLogin(String usuario, String senha){
        ParseUser.logInInBackground(usuario, senha, new LogInCallback() {
            @Override
            public void done(ParseUser user, ParseException e) {
                if ( e==null ){ //sucesso ao fazer login
                    Toast.makeText(LoginActivity.this, "Login realizado com sucesso! ", Toast.LENGTH_LONG).show();
                    abrirAreaPrincipal();
                } else { //erro ao logar
                    Toast.makeText(LoginActivity.this, "Login realizado com sucesso! " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });

    }


    public void abrirCadastroUsuario(View view){
        Intent intent = new Intent(LoginActivity.this, CadastroActivity.class);
        startActivity( intent );
    }

    private void verificarUsuarioLogado(){

        if ( ParseUser.getCurrentUser() != null ){ //se o usuario estiver logado
            //Enviar usuario para tela principal
            abrirAreaPrincipal();
        }
    }

    private void abrirAreaPrincipal(){
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity( intent );
        finish();
    }



}

How could use this Progressdialog so that when something goes wrong on the connection it closes automatically, because the error is already being displayed by Parse.

1 answer

0


Friend, you must call the progressDialog.dismiss(); when you want it to close, if you don’t do this will give a Warning on the logcat, "Liking window", since a screen of your current Activity leaked to the next, playing the responsibility of closing it to the next Activity.

Put the progressDialog.dismiss(); whenever it is leaving Activity or when there is error in some check, in case of error it would be nice you use a Toast to say what was the mistake.

  • I understand, and I’ve tried to, but nothing happens. Where and how I could insert in my code in the way it is already structured (changing as little as possible). As for the error already being displayed but in "verifying Login", that’s where I show, Progressdialog does nothing only shows the user when logging in.

  • You should always put it at the end of a process, for example, it finished checking if the user is logged in, before any action, you call the Dismiss. For example, everything worked out, before creating the Intente you close it, or if it goes wrong you close it and call a Toast, understand? A tip, don’t show android error message to user, use Logcat.

  • Do so, put Dismiss before the if in the 'check' method Played(). Why Progress should be closed anyway on this occasion. And try to implement your code using a second thread, it is not good practice to do this type of checking on the main thread. Asynctask would be the most suitable.

  • Solved, suddenly not in the best way, but it worked. I actually put in the ELSE of the Verifiarlogin() method; So it worked, IF IT DIDN’T WORK LOGIN, closes the Dialog.

  • Very good, but try to make these changes I told you, will greatly improve the performance of your app

Browser other questions tagged

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