Counter zeroing for no reason

Asked

Viewed 99 times

0

I have a method in which every time it is triggered, in addition to performing other operations, a certain counter cont should be incremented and then call another method where it will check that the counter is greater than zero.


The problem is that when it checks if it is greater than zero and must be when the method that increments is called, but the accountant automatically goes back to being zero for no reason and returns the command that must be performed when it is zero.


Method of incrementing the counter

public void enviar(View view) {
    Toast.makeText(this, "O cadastro foi realizado com sucesso", Toast.LENGTH_LONG).show(); 
    Intent telaInicial = new Intent(this, Principal.class);
    startActivity(telaInicial);
    this.cont++;
    qntProd();
}

Method that checks

public String qntProd() { 
    if(this.cont > 0) {
        return "Você possui " + this.cont + " produtos cadastrados";
    }else {
        return "Você não possui produtos cadastrados";
    }
}

The main class that calls the method qntProd()

public class Principal extends Activity {
    private TextView produtosCadastrados;
    private String retorno;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        produtosCadastrados = (TextView) findViewById(R.id.produtosCadastrados);
        //qntProd = produtos.qntProd();
        Cadastrar cadastrar = new Cadastrar();
        retorno = cadastrar.qntProd(cadastrar.cont);
        produtosCadastrados.setText(retorno);
        /*if(cadastrar.cont > 0) {
            produtosCadastrados.setText("Você possui " + cadastrar.cont + " produtos cadastrados.");
        }else {
            produtosCadastrados.setText("Você não possui produtos cadastrados.");
        }*/     
    }
    public void cadastra(View view) {
        Intent telaCadastra = new Intent(this, Cadastrar.class);
        startActivity(telaCadastra);
    }

}

Recommended code

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        produtosCadastrados = (TextView) findViewById(R.id.produtosCadastrados);
        Intent it = getIntent();
        int cont = it.getIntExtra("valor", 0);
        it.putExtra("valor", cont);
        if(cont > 0) {
            retorno = "Você possui " + String.valueOf(cont) + " produtos cadastrados";
        }else {
            retorno = "Você não possui produtos cadastrados";
        }
        produtosCadastrados.setText(retorno);

    }

1 answer

1

Error: When you finish executing the send method you open a new App and inside your Oncreate method you instantiate again the Register class (new Register()) with this the counter is created again with 0 and this is always repeated, you do not save the counter, simply creates another instance and tries to check, ie never your counter will be higher than 0.

Suggestion: Since you are not persisting the data, I do not advise creating a new Activity, every time it enters the send method return a Boolean, if the return method TRUE means that Activity can be cleaned, with this you clean the data from the screen and leave the counter in Activity, with that the counter will be a single instance.

Browser other questions tagged

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