0

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..

colymore
  • 11,776
  • 13
  • 48
  • 90

3 Answers3

1

Try setting following attribute to the TextView :

<TextView 
 ... 
 android:freezesText="true" />

You can find documentation on it here. Also try reading this post.

Community
  • 1
  • 1
slezadav
  • 6,104
  • 7
  • 40
  • 61
  • If i put this, when i rotate the screen i can see the text, but this dont change. In onPreExecute i have a tvprogreso.settext("text"), and i see text always. Any other idea? – colymore Nov 08 '12 at 13:38
1

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)

Community
  • 1
  • 1
Vajk Hermecz
  • 5,413
  • 2
  • 34
  • 25
  • And can i update my "new" textView? I have a gallery, with an adapter, if i update the textview same time the gallery will works?I cant block the orientation, and i need when rotate the activity is destroyed because i change the layout. – colymore Nov 08 '12 at 13:40
  • The quick and dirty solution is adding `android:configChanges="orientation|screenSize"` for your activity in manifest. More on the topic can be found [here](http://developer.android.com/guide/topics/resources/runtime-changes.html) – Vajk Hermecz Nov 08 '12 at 14:03
  • This latter approach enables orientation change, but without recreating the Activity... Isn't this what you actually need? You haven't mentioned in the original question that you cannot modify the manifest file... – Vajk Hermecz Nov 08 '12 at 20:49
  • I can modify the manifest but i need recreate the activity when the orientation change because i need to load other layout. – colymore Nov 12 '12 at 07:54
  • In that case, ideally, asyncTask should be independent from your Activity. E.g.: You should create a Service, instruct it from your Activity's onCreate to start dnload. Register a callback, or do polling if the result is availabe. After rotation, onCreate would instruct the service to continue dnloading, but as it is already finished/ongoing, you can skip creating a new asynctask there... Simpler approach is to keep asynctask in activity, but store results of asynctask in a singleton, and poll from there. – Vajk Hermecz Nov 12 '12 at 08:03
0

I just declared the TexView field to be static. It makes sense too. When it recreates the activity, the old variable is replaced with new one. Earlier on screen rotation it lost it's reference to the textview, but because of the static notation only one instance is kept and forces the it to use new one.

In my case instead of a AsycTask, I had a listener patterns in a dialog Fragment returning an updated item which I was using in the container activity to update the view.

That way you can use build in rotation handler as well as no need to block the view to portrait view.

neelabh
  • 479
  • 6
  • 19