Android Application and AsyncTask basics

Original Article: http://www.screaming-penguin.com/node/7746

public class Main extends Activity {
private static final String NAME = "NAME";
private EditText input;
private Button saveButton;
private Button deleteButton;
private TextView output;
private MyApplication application;
@Override
public void onCreate(final Bundle savedInstanceState) {
Log.d(MyApplication.APP_NAME, "onCreate");
super.onCreate(savedInstanceSt<wbr>ate);
this.setContentView(R.layout.m<wbr>ain);
// get "Application" object for shared state or creating of expensive resources - like DataHelper
// (this is not recreated as often as each Activity)
this.application = (MyApplication) this.getApplication();
// inflate views
this.input = (EditText) this.findViewById(R.id.in_text<wbr>);
this.saveButton = (Button) this.findViewById(R.id.save_bu<wbr>tton);
this.deleteButton = (Button) this.findViewById(R.id.del_but<wbr>ton);
this.output = (TextView) this.findViewById(R.id.out_tex<wbr>t);
// initially populate "output" view from database
new SelectDataTask().execute();
// save new data to database (when save button is clicked)
this.saveButton.setOnClickList<wbr>ener(new OnClickListener() {
public void onClick(final View v) {
new InsertDataTask().execute(Main.<wbr>this.input.getText().toString(<wbr>));
Main.this.input.setText("");
}
});
// delete all data from database (when delete button is clicked)
this.deleteButton.setOnClickLi<wbr>stener(new OnClickListener() {
public void onClick(final View v) {
new DeleteDataTask().execute();
}
});
}
@Override
public void onSaveInstanceState(final Bundle b) {
Log.d(MyApplication.APP_NAME, "onSaveInstanceState");
if ((this.input.getText().toStrin<wbr>g() != null) && (this.input.getText().toString<wbr>().length() > 0)) {
b.putString(Main.NAME, this.input.getText().toString(<wbr>));
}
super.onSaveInstanceState(b);
  }
@Override
public void onRestoreInstanceState(final Bundle b) {
Log.d(MyApplication.APP_NAME, "onRestoreInstanceState");
super.onRestoreInstanceState(b<wbr>);
String name = b.getString(Main.NAME);
if (name != null) {
// use onSaveInstanceState/onRestoreI<wbr>nstance state to manage state when orientation is changed (and whenever restarted)
// many views already do this automatically, so this is a silly example, but you could save your own state here too
this.input.setText(name);
}
}
private class InsertDataTask extends AsyncTask<String, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(Main.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Insert<wbr>ing data...");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Void doInBackground(final String... args) {
Main.this.application.getDataH<wbr>elper().insert(args[0]);
return null;
}
// can use UI thread here
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
// reset the output view by retrieving the new data
// (note, this is a naive example, in the real world it might make sense
// to have a cache of the data and just append to what is already there, or such
// in order to cut down on expensive database operations)
new SelectDataTask().execute();
}
}
private class SelectDataTask extends AsyncTask<String, Void, String> {
private final ProgressDialog dialog = new ProgressDialog(Main.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Select<wbr>ing data...");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected String doInBackground(final String... args) {
List<String> names = Main.this.application.getDataH<wbr>elper().selectAll();
StringBuilder sb = new StringBuilder();
sb.append("Names in database:\n");
for (String name : names) {
sb.append(name + "\n");
}
return sb.toString();
}
// can use UI thread here
protected void onPostExecute(final String result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
Main.this.output.setText(resul<wbr>t);
}
}
private class DeleteDataTask extends AsyncTask<String, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(Main.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Deleti<wbr>ng data...");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Void doInBackground(final String... args) {
Main.this.application.getDataH<wbr>elper().deleteAll();
return null;
}
// can use UI thread here
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
// reset the output view by retrieving the new data
// (note, this is a naive example, in the real world it might make sense
// to have a cache of the data and just append to what is already there, or such
// in order to cut down on expensive database operations)
new SelectDataTask().execute();
}
}
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.