I have a few fragments in my activity. Each fragment makes a call to an AsyncTask which loads data from the local DB in the onCreate()
method like this:
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
(new DataLoaderTask()).execute();
...
}
I have three fragments that do this. In doing so, the fragments do not finish drawing their UI until the DataLoaderTask completes. Why is this? I tried changing the call to this and I no longer have the issue:
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
(new DataLoaderTask()).execute();
}
}
...
}
Why does making the call inside a Runnable passed into a Handler work? Shouldn't the AsyncTask be running in the background anyway and hence the UI should get drawn before it completes?
Thanks for your help.
UPDATE: Adding more info. Here's the constructor of DataLoaderTask():
public DataLoaderTask(Object object) {
mObject = object;
mListeners = new ArrayList<OnUpdateListener>();
}
DataLoaderTask does NOT override the onPreExecute()
method. It does override the onPostExecute()
method. I've timed my onPostExecute()
method and it takes approx ~2ms. All it's doing is updating some objects and calling a method on any Listeners provided.
UPDATE: Here's the full onPostExecute()
method:
@Override
protected void onPostExecute(Object result) {
synchronized(mDataManagerLock) {
if (Config.isLogging()) {
Log.i(TAG, "*** *** *** *** Finished syncing with database (onPostExecute()).");
}
if (mObject instanceof Playlist && result instanceof Playlist) {
if (((Playlist) result).isMyPlaylist()) {
synchronized(mTmpMyPlaylist) {
if (mTmpMyPlaylist != null && !mTmpMyPlaylist.isEmpty()) {
((Playlist) result).addPlaylistActions(mTmpMyPlaylist.getPlaylistActions());
mTmpMyPlaylist.clear();
}
}
}
if (mergeLocalPlaylistChanges((Playlist) result) && Config.isLogging()) {
Log.i(TAG, "Local playlist changes during sync merged.");
} else if (Config.isLogging()) {
Log.i(TAG, "No local playlist changes were made during sync.");
}
((Playlist) mObject).replace((Playlist) result);
putPlaylist((Playlist) mObject, null /*newClips*/);
} else if (mObject instanceof Catalog && result instanceof Catalog) {
((Catalog) mObject).replace((Catalog) result);
putCatalog((Catalog) mObject, null);
} else if (mObject instanceof NotificationsList) {
// We've synced NotificationsList in Memory with Disk.
mNotificationsList.setLastNetworkFetchTime(mApp.getNotificationsNetworkFetchTime());
} else if (mObject instanceof SetsList) {
// We've synced SetsList in Memory with Disk.
mSetsList.setLastNetworkFetchTime(mApp.getExploreNetworkFetchTime());
}
if (Config.isLogging()) {
Log.i(TAG, mObject.getClass().getSimpleName() + " after sync with database: " + mObject);
}
}
if (Config.isLogging()) {
Log.i(TAG, "Finished syncing in memory object/list with database.\n *** Time taken: " + (System.currentTimeMillis() - mStartTime)/1000 + " milliseconds.");
}
if (mListeners != null) {
for (OnUpdateListener listener : mListeners) {
if (listener != null) {
listener.onUpdated();
}
}
}
}