I am following tutes from codelearn and trying to create an AsyncTask that generates tweets and executes another AsyncTask to write to the cache file. I have Thread.sleep, so the user interface, on first boot, waits until tweets are written to the cache file. First I execute AysncTask new AsyncWriteTweets(this.parent).execute(tweets);, then sleep for 10 seconds.
But in logcat, I see that AsyncWriteTweets also starts after 10 seconds of sleep. Therefore, onPostExecute is executed before the tweets are written to the cache file, giving a blank screen.
public class AsyncFetchTweets extends AsyncTask<Void, Void, Void> {
private TweetListActivity parent;
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
ArrayList[] temp;
public AsyncFetchTweets(TweetListActivity parent){
this.parent = parent;
}
@Override
protected Void doInBackground(Void... params) {
int result = 0;
Log.d("ASync", "Calling asycn");
for (int i=0;i<4;i++){
Tweet tweet = new Tweet();
tweet.setTitle("Title Async Very New" + i);
tweet.setBody("Body text for tweet no " + i);
tweets.add(tweet);
}
new AsyncWriteTweets(this.parent).execute(tweets);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
Log.d("Async", "on Post execute");
this.parent.renderTweets();
}
}
PS: My guess: AsyncTask should create a new thread, therefore Thread.sleep in the parent should not stop the children. If it is otherwise please advise how I can solve this problem.