Use a lifecycle aware async task instead of a loader to open collection #4972
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes
Fixes #4938 and is related to #4937 and #4939
Supersedes #4950
Approach
The new lifecycle support library makes it pretty easy to build lifecycle aware components, and Loaders have now been deprecated in favor of that library. So instead of using an
AsyncTaskLoader
as the base class, I've used anAsyncTask
and added some simple logic to ensure thatonCollectionLoaded()
is not called when theActivity
is stopped.I was originally considering to use the new
ViewModels
plusLiveData
approach, but quickly realized that's overkill for the simple task we're trying to achieve here.In the future I think we should probably refactor the guts of this into a more general
LifecycleAwareAsyncTask
and use that more liberally in our code base.How Has This Been Tested?
I did some basic sanity checks on the simulator with a phone and tablet profile to make sure that all Activities are starting up correctly and that no crashes occur when the Activity is paused or stopped while the collection is loading.
Learning (optional, can help others)
https://developer.android.com/topic/libraries/architecture/lifecycle
This was pretty good as well:
https://developer.android.com/topic/libraries/architecture/guide.html