Reputation: 913
I have a string that I want to update using AsyncTask class here is my code:
public class MainActivity extends Activity{
private String str = "oldString";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getString();
}
private void getString(){
new CustomTask().execute();
}
private class CustomTask extends AsyncTask<Void, Void, String>{
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
Log.i("Lesson3", "doInBackground method");
str = "newString";
return "someString";
}
protected void onPostExecute(String s){
Log.i("Lesson3", "onPostExecute method");
}
}
onPostExecute method is not called, thats the problem??
Thanks
Upvotes: 2
Views: 4978
Reputation: 1060
How about having our own AsyncTask ?
Please have a look.
public abstract class CustomAsyncTask<Params, Progress, Result> {
private boolean isFinished = false;
private Result res;
protected void onPreExecute() {
}
protected void onPostExecute(Result result) {
}
protected void onProgressUpdate(Progress progress) {
}
protected abstract Result doInBackground(Params...params);
private void CheckAndUpdateUI()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(isFinished)
{
onPostExecute(res);
}
else
CheckAndUpdateUI();
}
},500);
}
public void execute(final Params...params)
{
onPreExecute();
//
CheckAndUpdateUI();
isFinished = false;
//
new Thread(new Runnable() {
@Override
public void run() {
res = doInBackground(params);
isFinished = true;
}
}).start();
}
}
Upvotes: 0
Reputation: 9574
Answer is simple,
@Override
protected void onPostExecute(String s){
Log.i("Lesson3", "onPostExecute method");
}
You forgot your @Override
the execute calls onPostExecute
of the super class since it has not been overridden as yet.
Upvotes: 2
Reputation: 69228
Why have you decided it doesn't run? Following code works just fine (fixed syntax errors in yours):
package com.stackoverflow;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class Test extends Activity {
private String str = "oldString";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getString();
}
private void getString() {
new CustomTask().execute();
}
private class CustomTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
Log.i("Lesson3", "doInBackground method");
str = "newString";
return "someString";
}
protected void onPostExecute(String s) {
Log.i("Lesson3", "onPostExecute method");
}
}
}
Upvotes: 0
Reputation: 1014
public class MainActivity extends Activity {
private String str = "oldString";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getString();
}
private void getString() {
new CustomTask().execute();
}
private void postExecuteCallback() {
Log.i("Lesson3", "String after callback: " + str );
}
private class CustomTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
Log.i("Lesson3", "doInBackground method");
str = "newString";
return "someString";
}
protected void onPostExecute(String s) {
Log.i("Lesson3", "onPostExecute method");
postExecuteCallback();
}
}
}
Upvotes: 0