Correctly Handling Fragment Interactions From An Async-task
I am trying to refactor my fragment-heavy code with safer async tasks as I had noticed some occasional crashes. Now I am slightly better educated after some searches I've noticed m
Solution 1:
from the doc:
public final boolean isAdded ()
Returntrueif the fragment is currently added to its activity.
So the activity could not be null.
edit:
To achieve a long running task I recommend you to use an IntentService (if the task do complex local operations, otherwise an AsyncTask is ok), save data in DB (or in other forms, for example in SharedPreferences if it is a small chunk of informations) and propagate the logic using a LocalBroadcast. You could also manage a check in onCreate() callback of the fragment so if it was detached when the task was expired, it could retrieve the data persisted and manage them properly; after that, you could flush the data stored previously.
Post a Comment for "Correctly Handling Fragment Interactions From An Async-task"