Or you can simple add something like android:configChanges="orientation|keyboardHidden" to your activity in the AndroidManifest.xml and then your activity won't be killed and restarted. You can then override the onConfigurationChanged method to see when these things happen if you want to know.
Check this for more info: http://developer.android.com/guide/topics/manifest/activity-element.html#config
If you are using properly flowing layouts, most of the time you won't even have to do anything at all. If you need specific layouts for portrait and landscape, the android:configChanges may make things more complicated than just saving off the status of your AsyncTask and restarting it from that point.
Steven
Studio LFP
http://www.studio-lfp.com
On Monday, October 3, 2011 12:25:58 PM UTC-5, Bluemercury wrote:
Hi! im using an Async-Task and currently im using runOnUiThread in the doInBackground method to refresh activities UI, this seems to be working but on rotation it loses the data and im usng the getLastNonConfigurationInstanc--e and the onRetainNonConfigurationInstan ce methods, here's the parent class method that calls the async task: /*** do asynctask for background work*/public void doAsyncTask(){//get task back in case of rotationtask= (QuadrosMobileActivityTask)getLastNonConfigurationInstanc e(); if(task==null){task= new QuadrosMobileActivityTask(this); task.execute();//add to the set of tasksQuadrosMobileApplicationContext appliContext= ( QuadrosMobileApplicationContex t)getApplicationContext(); appliContext.getAsyncTasks().add(task); }else{task.attach(this);}}Here's the async task as normal class(non inner class) main methods;@Overrideprotected void onPreExecute() {Logger.write("QuadrosMobileActivityTask ", " AsyncTask pre execution "+ "Current Activity: "+activity.getClass().getName( ), Logger.INFO); if(QuadrosMobileActivity.themeId!=R.style. CustomThemeNoHeader){ activity.findViewById(R.id.progressView).setVisibility( ProgressBar.VISIBLE); }QuadrosMobileActivity.progressBarstate=ProgressBar. VISIBLE; }@Overrideprotected String doInBackground(Void... params) {Logger.write("QuadrosMobileActivityTask ", "initialized", Logger.INFO); return activity.updateResultsInUi();}@Overrideprotected void onPostExecute(String result) {Logger.write("QuadrosMobileActivityTask ", " AsyncTask post execution "+"Current Activity: "+activity.getClass().getName( ), Logger.INFO); if(activity!=null)Toast.makeText(activity, result, Toast.LENGTH_LONG).show();//remove this task from collectionQuadrosMobileApplicationContext appliContext= QuadrosMobileApplicationContex t.getInstance(); appliContext.getAsyncTasks().remove(this); //check if there's more tasks in the collectionif(appliContext.getAsyncTasks().isEmpty()){ if(QuadrosMobileActivity.themeId!=R.style. CustomThemeNoHeader){ activity.findViewById(R.id.progressView).setVisibility( ProgressBar.GONE); }QuadrosMobileActivity.progressBarstate=ProgressBar. GONE; }}And the sub activities updateResultsInUi() method:runOnUiThread(new Runnable() {@Overridepublic void run() {((TextView)findViewById(R.id.nome_value)).setText(quadro. getNome()); //((TextView)findViewById(R.id.nif_value)).setText(quadro. getDadosPessoais(). getNumeroContribuinte()); ((TextView)findViewById(R.id.datanasc_value)).setText( DateTimeZoneUtils.getInstance( ).sqlLiteDateTimeFormat( quadro.getDadosPessoais(). getDataNascimento())); ((TextView)findViewById(R.id.provincia_value)).setText( quadro.getResidencia(). getProvincia()); ((TextView)findViewById(R.id.municipio_value)).setText( quadro.getResidencia(). getMunicipio()); ((TextView)findViewById(R.id.comuna_value)).setText(quadro. getResidencia().getComuna()); ((TextView)findViewById(R.id.bairro_value)).setText(quadro. getResidencia().getBairro()); ((TextView)findViewById(R.id.rua_value)).setText(quadro. getResidencia().getRua()); }});Can i use the runOnUIThread method inside the doInBackground method of AsyncTask??regards,
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
No comments:
Post a Comment