I'm with an app that I want to use AsyncTask so that when I do a task that takes some time, I'll load the message loading ... and once I finish this task disappear the message and continue to run the app normally. I have never used the Asynctask before, so I was experimenting but I have not been successful. In theory it seemed simple but it is more complex than I thought. I leave you the code that to see the structure, know that I am a novice in this.
Thank you very much.
public class RegistracionActivity extends AppCompatActivity {
private EditText inputFirstName, inputSurname, inputEmail, inputPass,
inputMobile,
inputPostCode, inputDateOfBirth;
class MyTask extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
setupProgressDialog();
}
@Override
protected String doInBackground(Void... voids) {
registracion();
return "Terminado";
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registracion);
// new MyTask().execute();
setupActionBar();
setupProgressDialog();
setupInputs();
}
private void registracion(){
UserApp usuario = new UserApp();
dialog.show();
String email = inputEmail.getText().toString();
usuario.setEmail(email);
String firstName = inputFirstName.getText().toString();
usuario.setFirstName(firstName);
String surName = inputSurname.getText().toString();
usuario.setSurName(surName);
String date = inputDateOfBirth.getText().toString();
usuario.setDateOfBirth(date);
String prefix = ccp.getSelectedCountryCodeWithPlus();
String phone = inputMobile.getText().toString();
usuario.setMobile(prefix+phone);
String code = inputPostCode.getText().toString();
usuario.setPostCode(code);
usuario.setBalance(0.0);
if(checkData(usuario)){
registracionWithFirebase(user);
}
}
Button bt1 = (Button) findViewById(R.id.register_button);
bt1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new MyTask().execute();
}
});
private void setupProgressDialog() {
dialog = new ProgressDialog(this); // this = YourActivity
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Cargando. porfavor espere...");
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
}
}