Use ProgressDialog when logging in

1

I'm trying to insert a ProgressDialog into the Login screen of my Android APP.

In parts it works, only when the login is successful, but when something is wrong as a password, 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 I use this ProgressDialog so that when something goes wrong on the connection, it automatically closes because the error is already being displayed by Parse.

    
asked by anonymous 30.05.2017 / 22:21

1 answer

0

Friend, you should call progressDialog.dismiss(); when you want it to close, if you do not it will give a Warning in the logcat, "liking window", since a screen of your current Activity has leaked to the next one, responsibility to close it for the next Activity.

Put progressDialog.dismiss(); whenever you leave the activity or when you make a mistake in some verification, in case of error it would be nice to use a Toast to say what was the error.

    
31.05.2017 / 13:19