Issue
I am new to andorid programming I've just stucked into a problem to call returning value function inside AsyncTask's doInBackground()
Simple question is How to wait for AsyncTask to complete and then execute return statement without freezing UI, i've also studied onPostExecute()
but it doesn't solve problem.
following is sample code
public String hello() {
String result = null;
//calling asynctaske execute method
retrun result;
}
Solution
For myself i do this with a callback Function, that i invoke after onPostExecute.
public AsyncUnzip(Activity ctx, Observer callback) {
this.ctx = ctx;
this.callback = callback;
}
and
@Override
protected void onPreExecute() {
super.onPreExecute();
dia = new ProgressDialog(ctx);
dia.setTitle("Bitte warten");
dia.setMessage("Geodatenpaket wird entpackt...");
dia.setCancelable(false);
dia.show();
}
and
@Override
public void onPostExecute( Boolean result ) {
super.onPostExecute(result);
dia.dismiss();
callback.update(null, returnFolder);
System.out.println("Unzipped to: " + returnFolder.getName() );
}
In that case your call of Async Task would look like that:
AsyncUnzip unzipThread = new AsyncUnzip(ImportActivity.this, new Observer() {
@Override
public void update( Observable observable, Object data ) {//your code invoked after Async Task
} });
unzipThread.execute(selectedFile); //Start Unzip in external Thread.
Note: This is a quick and diry solution with a annonymous Observer implementation and without an observable.
Answered By - Matthias H
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.