i have a asyntask, and in progress update i set text a TextView, this works fine, but then i rotate the screen, the textview reset and set “”(This is the initial value), in the postupdate i have a new findviewbyid(r.id.tvProgreso); and it update the textview but the text will be same.
This is my onCreate method:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_incio);
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
ActionBar actionBar = getActionBar();
actionBar.hide();
}
contexto = getApplicationContext();
datos = new ArrayList<Integer>();
recojeDatosDeSesion(savedInstanceState);
adaptador = new AdaptadorImagenes(contexto);
if (getLastNonConfigurationInstance() != null) {
adaptador = (AdaptadorImagenes) getLastNonConfigurationInstance();
}
// Layout
etEmail = (EditText) findViewById(R.id.etEmail);
etPassword = (EditText) findViewById(R.id.etPassword);
tvProgreso = (TextView) findViewById(R.id.tvProgreso);
listaHorizontalImagenes = (Gallery) findViewById(R.id.galeria);
listaHorizontalImagenes.setGravity(Gravity.LEFT);
listaHorizontalImagenes.setAdapter(adaptador);
listaHorizontalImagenes.setSelection(0);
btnDescargar = (Button) findViewById(R.id.btnDescargar);
clickDescargar = new OnClickListener() {
@Override
public void onClick(View v) {
if (!descargaActiva) {
email = etEmail.getText().toString();
password = etPassword.getText().toString();
if (validaDatos(email, password)) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
new DescargaFotos().execute();
descargaActiva = true;
Toast.makeText(contexto, "Comienza la descarga.", Toast.LENGTH_SHORT).show();
tvProgreso.setText("Descargando fotos..");
}
} else {
Toast.makeText(contexto, "Descarga en proceso", Toast.LENGTH_LONG).show();
}
}
};
if (descargaActiva)
btnDescargar.setOnClickListener(null);
else
btnDescargar.setOnClickListener(clickDescargar);
}
and the progressUpdate in asyntask
@Override
protected void onProgressUpdate(Integer... progreso) {
// Actualizo el progreso
progresoImagenes = progreso[0];
actualizaTexto(progreso[0]);
adaptador.addItem(progreso[0]);
}
private void actualizaTexto(Integer progreso) {
tvProgreso = (TextView) findViewById(R.id.tvProgreso);
tvProgreso.setText(progreso.toString() + " de " + numeroDeFotos + " fotos.");
}
When i debug, i see the textviex.settext with te correct text, but this not change..
Rotation by default recreates your activity. It actually creates a new Activity objects, and displays that on the screen. The asynctask still refers to the old Activity which is not visible anymore, and updates the TextView in that….
You can either handle rotation yourself, thus Activity wont be destroyed as described here, or you can constraint your activity to be only available in portrait mode (add android:screenOrientation=”portrait” to the activity tag)