Well, I'm developing an Android application and at the moment, I've encountered a problem for which I can not find a solution, I'll probably be doing something wrong, I hope someone can help me. The problem consists of the following: I have an 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 data through that code. So far so good. But, when I move from the main activity to a second activity, it repeats the same process again and the alertdialog is opened again to request a code. I want the alertdialog to only appear when starting the app. Can someone help me? Below is the code of the OnCreate method of the 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 in 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.