Android Alertdialog

Asked

Viewed 365 times

1

Good, I’m developing an Android application and right now, I found a problem for which I can’t find a solution, I’ll probably be doing something wrong, I hope someone can help me. The problem is this: I have a alertdialog that opens when the application opens and asks for a code. The user enters the code and the application checks the code in the database (mysql) and receives the user’s data through that code. So far so good. But when I step from the main Activity to a second Activity, it repeats the same process again and the alertdialog opens again asking for a code. I want alertdialog to only appear when starting the app. Can anyone help me? Below is the Oncreate method code from Main Activity.

//Cria Formulário de pedido
    AlertDialog.Builder builder = new 
AlertDialog.Builder(MainActivity.this);
    //Titulo do AlertDialog
    builder.setTitle("Insira o seu código:");

    // Set up the input
    final EditText input = new EditText(MainActivity.this);

    input.setInputType(InputType.TYPE_CLASS_TEXT | 
InputType.TYPE_CLASS_TEXT);
    builder.setView(input);

    // Set up the buttons
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            txtstatus.setText("A verificar código . . .");
            //Recebe Código
            at = input.getText().toString();

                //Guarda o Código e o Url
                at = "at=" + at;
                url_nome_user = "/user_nome.php";

                //Verifica e recolhe os dados do Código
                BackgroundTask backgroundTask = new BackgroundTask();
                backgroundTask.execute(url_nome_user);
                txtstatus.setText("A receber Dados associados . . .");
            }
        }
    });
    builder.setNegativeButton("Cancelar", new 
DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //Se o Utilizador cancelar
            dialog.cancel();
            Toast.makeText(MainActivity.this,"Falha ao introduzir Código!", 
Toast.LENGTH_SHORT).show();
            txtstatus.setText("Falha ao introduzir Código!");
            finish();
        }
    });
    //Desativar Botão Back
    builder.setCancelable(false);
    //cria o AlertDialog
    AlertDialog alerta = builder.create();
    //Exibe
    alerta.show();

Now, on the Backgroundtask:

class BackgroundTask extends AsyncTask<String, Void, String>{
    @Override
    protected void onPreExecute() {

    }
    @Override
    protected String doInBackground(String... urls) {
        //Verifica na API o Código de Acesso
        //Dados User
        nome_user = Conexao.postDados(url_nome_user,at);
        return nome_user;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {

        //Mostra o Resultado
        Toast.makeText(MainActivity.this,result, Toast.LENGTH_LONG).show();
        MudardeActivity();
    }
  }

public void MudardeActivity(){
 /* Create an Intent that will start the Menu-Activity. */
Intent intent = new Intent(MainActivity.this,Inicio.class);
intent.putExtra("nome_user", nome_user);

MainActivity.this.finish();
startActivity(intent);

}

Can someone help me? Thank you.

  • The first passage only has the dialogue (which seems to be in order), has nothing else in this onCreate()? And the method of onCreate() of inicio.class? Anyway, there is not enough information (code) to find what might be wrong. You debug step by step, to try to understand what is happening?

  • After checking the Activity Start is not called? When it "repeats the same process again"?

  • Dear Rene Freak, there is no more code in onCreate. in the Oncreate class Inicio, there is nothing. only the layout of Activity. the application was supposed to open only the layout since there is no code.

  • Dear Ramaral, the Activity Inicio is called, but when it appears, the initial Activity alertdialog opens again.

  • Well, I managed to solve the problem, and I apologize in advance. I just rewrote all the code on a new project and it’s working :D. I’ve probably changed some definition on that project. I’m sorry. Thank you :)

No answers

Browser other questions tagged

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