Monday, October 3, 2011

[android-developers] Re: runOnUIThread method in doInBackground of Async Task...

If you aren't using the android:configChanges tag on your activity in the AndroidManifest.xml then you application is getting killed and restarted on rotation or keyboard changes like when someone slides out a hardware keyboard. When this happens, you need to save off the position in your AsyncTask and restart it back when the activity gets restarted.

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 getLastNonConfigurationInstance and the onRetainNonConfigurationInstance 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 rotation
task= (QuadrosMobileActivityTask)getLastNonConfigurationInstance();

if(task==null){
task= new QuadrosMobileActivityTask(this);
task.execute();
//add to the set of tasks
QuadrosMobileApplicationContext appliContext= (QuadrosMobileApplicationContext)getApplicationContext();
appliContext.getAsyncTasks().add(task);
}else{
task.attach(this);
}
}

Here's the async task as normal class(non inner class) main methods;

       @Override
protected 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;
}

@Override
protected String doInBackground(Void... params) {
Logger.write("QuadrosMobileActivityTask ", "initialized", Logger.INFO);
return activity.updateResultsInUi();
}

@Override
protected 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 collection
QuadrosMobileApplicationContext appliContext= QuadrosMobileApplicationContext.getInstance();
appliContext.getAsyncTasks().remove(this);

//check if there's more tasks in the collection
if(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() {

@Override
public 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