Error closing ProgressDialog

1
 public UserRest login(Usuario usuario,  final Context context){
    try {
        showProgress(context, "Aguarde", "Validando usuário...");

        UserService service =  ServiceGenerator.createService(UserService.class);
        Call<UserRest> call = service.login(usuario.getEmail(),usuario.getSenha());
        call.enqueue(new Callback<UserRest>() {

            @Override
           public void onResponse(Call<UserRest> call, Response<UserRest> response) {
               closeProgress();
               okhttp3.Response raw = response.raw();
               userRest = response.body();
               UsuarioDAO usuarioDAO = new UsuarioDAO(context);
               Usuario usuario = usuarioDAO.getUsuario();


               if (usuario != null  && userRest.getId()!=null) {
                   usuario.setFlagLogado("S");
                   usuarioDAO.controleLogin(usuario);
                   context.startActivity(new Intent(context, MenuActivity.class));
               }else {
                   new Handler().postDelayed(new Runnable() {
                       @Override
                       public void run() {
                           Toast.makeText(context, "Usuário não encontrado", Toast.LENGTH_SHORT).show();

                       }
                   }, 500);


               }

           }

           @Override
           public void onFailure(Call<UserRest> call, Throwable t) {
               closeProgress();
               Log.e(ConstantsTrocaae.TAG_LOG, t.getMessage());
               Toast.makeText(context, "Usuário não encontrado", Toast.LENGTH_SHORT).show();
           }
       });


    } catch (Exception e) {
        Log.e(ConstantsTrocaae.TAG_LOG, e.getMessage());

    }finally {
        closeProgress();
    }
    return userRest;

}

I would like to know why not add my progress when I do not think the user? The strangest thing is that when he finds it, he disappears.

private void showProgress(Context context, String titulo, String mensagem){
    progressDialog = new ProgressDialog(context);
    progressDialog.show(context, titulo, mensagem);
    progressDialog.setCancelable(true);

}

private void closeProgress(){
    if(progressDialog!=null && progressDialog.isShowing()){
        progressDialog.dismiss();
        progressDialog = null;
    }
}
    
asked by anonymous 16.06.2016 / 13:31

2 answers

0

Speak Fabio,

Do you see any error in logCat?

I imagine the problem is in this part:

finally {
   closeProgress();
}

Try to remove this part, it might solve.

Hugs.

    
16.06.2016 / 14:59
0

I noticed that in this case it only closes when I call a new Intent and call another Actvity, I keep it that way, in my case I have Activity calling the login method in another class:

@OnClick(R.id.entrar)
public void login(View v) {

    final Usuario usuario = new LoginHelper(this).pegaUsuarioFormulario();

    try {

        if(validaUsuario(email,senha)){
            new UsuarioRetrofit().login(usuario,MainActivity.this);
        }

    } catch (Exception e) {
        Log.e(ConstantsTrocaae.TAG_LOG, e.getMessage());
    }
}
    
16.06.2016 / 22:29