ProgressDialog closing before Second Activity opens

1

I have an Activity called Login_Activity, when the user logs in, I want a ProgressDialog to appear, until the other Activity is fully loaded, because since I'm going to put some things to execute in the onCreate method of the second Activity, the screen a time locked with nothing on the screen for the user.

Follow the main Login_Activity class ...

package com.example.patrickcamargo.rmpgseguros.Apresentacao;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
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.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.patrickcamargo.rmpgseguros.Conexao.Conexao;
import com.example.patrickcamargo.rmpgseguros.Conexao.ConexaoInterface;
import com.example.patrickcamargo.rmpgseguros.Modelo.ErrosBD;
import com.example.patrickcamargo.rmpgseguros.Modelo.JDialogCarregando;
import com.example.patrickcamargo.rmpgseguros.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

public class Login_Activity extends AppCompatActivity implements ConexaoInterface, Serializable{

    EditText txt_loginEmail, txt_loginSenha;
    Button btn_loginEntrar;
    Login_Activity login_activity;

    JDialogCarregando jDialogCarregando = new JDialogCarregando();

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

        login_activity = this; //VARIAVEL CRIADA PARA PASSAR O PARAMETRO PARA A CLASSE CONEXÃO
        txt_loginEmail = (EditText)findViewById(R.id.txt_loginEmail);
        txt_loginSenha = (EditText)findViewById(R.id.txt_loginSenha);
        btn_loginEntrar = (Button) findViewById(R.id.btn_loginEntrar);

        btn_loginEntrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jDialogCarregando.inicializar(login_activity,"Carregando...");
                Conexao conexao = new Conexao(getApplicationContext(), login_activity);
                String parametrosUrl = "email=" + txt_loginEmail.getText() + "&senha=" + txt_loginSenha.getText();
                conexao.execute("TCC/json_logarCliente.php",parametrosUrl);
            }
        });
    }

    @Override
    public void depoisDownload(String result) throws JSONException {
        if( result == null) {
            //SE O RESULTADO FOR NULL, QUER DIZER QUE NÃO OBTEVE SUCESSO NA COMUNICAÇÃO COM O SERVIDOR
            //ENTÃO SE EXIBE A MENSAGEM DE ERROR
            Conexao conexao = new Conexao(this, this);
            conexao.exibeSemConexao(this);
        } else if (result.contains("ERROR")) {
            //ERROR 147 QUER DIZER QUE O LOGIN ESTÁ ERRADO, ENTÃO RECEBE-SE A MENSAGEM DE DADOS INCORRETOS
            ErrosBD errosBD = new ErrosBD();
            Toast.makeText(this,errosBD.ErrosBD(result),Toast.LENGTH_LONG).show();
            txt_loginSenha.setText("");
        } else {
            //CRIANDO VARIAVEIS PARA PASSAR COMO PARAMETROS PARA A ACTIVITY PRINCIPAL
            String[] vt_carro = new String[10], vt_placa = new String[10], vt_cor = new String[10], vt_ano = new String[10];
            String p_email = null, p_nome = null, p_cpf = null, p_rg = null, p_cnh = null, p_id = null;

            //CRIANDO ARRAY COM O RESULTADO
            JSONArray minhaArray = new JSONArray(result);
            JSONObject obj;
            for (int i = 0; i < minhaArray.length(); i++) {
                obj = new JSONObject(minhaArray.getString(i));
                if (i == 0) {
                    p_nome = obj.getString("Nome");
                    p_email = obj.getString("Email");
                    p_rg = obj.getString("RG");
                    p_cnh = obj.getString("CNH");
                    p_cpf = obj.getString("CPF");
                    p_id = obj.getString("FK_Pessoa");
                } else {
                    vt_carro[i-1] = obj.getString("Marca") + " - " + obj.getString("Modelo");
                    vt_placa[i-1] = obj.getString("Placa");
                    vt_cor[i-1] = obj.getString("Cor");
                    vt_ano[i-1] = obj.getString("Ano");
                }
            }
            //PASSANDO OS PARAMETROS PARA A ACTIVITY PRINCIPAL
            Intent intent = new Intent(Login_Activity.this, MenuInicial.class);
            intent.putExtra("p_nome", p_nome);
            intent.putExtra("p_email", p_email);
            intent.putExtra("p_cpf", p_cpf);
            intent.putExtra("p_cnh", p_cnh);
            intent.putExtra("p_rg", p_rg);
            intent.putExtra("vt_carro", vt_carro);
            intent.putExtra("vt_placa", vt_placa);
            intent.putExtra("vt_cor", vt_cor);
            intent.putExtra("vt_ano", vt_ano);
            startActivity(intent);

        }
        jDialogCarregando.finalizar();
    }
}

Follow the class that displays Progress Dialog

package com.example.patrickcamargo.rmpgseguros.Modelo;

import android.app.ProgressDialog;
import android.content.Context;

public class JDialogCarregando {
    private ProgressDialog progress;

    public void inicializar(Context context, String messageConexao)
    {
        progress = new ProgressDialog(context);
        progress.setMessage(messageConexao);
        progress.setCancelable(false);
        progress.show();
    }
    public void finalizar()
    {
        progress.dismiss();
    }

}

And finally, the second activity that will be called

package com.example.patrickcamargo.rmpgseguros.Apresentacao;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.example.patrickcamargo.rmpgseguros.Modelo.JDialogCarregando;
import com.example.patrickcamargo.rmpgseguros.R;

import java.io.Serializable;

public class MenuInicial extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    JDialogCarregando jDialogCarregando = new JDialogCarregando();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_inicial);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_inicial, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;                                                                                 

If you had a way to call the jDialogLoading.finalize () method only when the other activity was running it would help ....

    
asked by anonymous 30.11.2017 / 19:05

1 answer

1

The screen in the second activity gets stuck because, in fact, it is loading something. You should initialize your Progress on the second activity the same way you did on the first.

Notice that by sending the parameters via intent you end progress and thus can not be running while the other activity loads. So when you make the request to display the second activity it will already be running. What you should do is to use your Progress method again:

Declaring your role:

JDialogCarregando jDialogCarregando = new JDialogCarregando();

And then calling:

jDialogCarregando.inicializar(MenuPrincipal.this, "Carregando...");

Then just end your function where you think the load ends:

jDialogCarregando.finalizar();
    
27.04.2018 / 00:38