AsyncTask

I had a use case for this, and was helped greatly (finally) by This page which has a wonderful diagram concerning how the parameters to an AsyncTask work. It also contains the example that shows to move the onPostExecute code into the activity. (see the green code below.)


cat res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="drowtercespot" />
    <Button
		android:id="@+id/send_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send" />
	<TextView
		android:id="@+id/the_message"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="message:" />
	<TextView
		android:id="@+id/mousetrap"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="Ver: 5" />
</LinearLayout>

The code in green is usually placed in the AsyncTask class code. By placing it in the Activity code there is better context for understanding the code. It also shows that more than one Activity can use the same AsyncTask and also that the same AsyncTask can be used for more than one purpose.

Imagine an AsyncTask that passes data to a server and sends the response to the Activity that called it. Suppose that it has two buttons, one for sending data to server A, The other for sending data to server B. Only one async task is needed!

The green code is placed in the ClickListener code. It looks good there, the click initiates the process, the green code completes the process. It also ensures that no attempt is made to rerun an AsyncTask (which is a Bozo NoNo.) The AsyncTask is reinstantiated ech time the button is clicked.

cat msgScreen.java
package com.nthist.srvmsg;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class msgScreen extends Activity
{
	String TAG = "msgScreen";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final Button button = (Button) findViewById(R.id.send_button);
		button.setText("Submit");

		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.i(TAG, "Submit button clicked.");
//				Perform action on click 
				new aprocess() {
					@Override
					public void onPostExecute(String result) {
						TextView txt = (TextView) findViewById(R.id.the_message);
						txt.setText(result);
						((EditText) findViewById(R.id.edit_message)).setText( "" );
					}
				}.execute( new String(((EditText) findViewById(R.id.edit_message)).getText().toString().trim()), 
						new String( ( (TextView) findViewById(R.id.mousetrap) ) .getText().toString().trim() ) );
						
			}
		});
	}
}


cat aprocess.java
package com.nthist.srvmsg;

import android.util.Log;
import android.os.AsyncTask;

class aprocess extends AsyncTask
{
	String TAG = "aprocess";

	protected void onPreExecute (){
		super.onPreExecute();
		Log.d(TAG + " PreExceute","On pre Exceute......");
	}

	protected String doInBackground(String... params) {
		Log.d(TAG + " DoINBackGround","On doInBackground...");

		if( params[1] != null ) {
			return params[0] + " " + params[1] + " " + params[0] ;
		}

		String aa = params[0] + " " + params[0] ;
		return aa;
	}
}