I have previously had my App working with just activities and am now working on converting to fragments in order to improve the UI.
Previously my Activity
started an AsyncTask
and passed in itself to be used as the Context
when certain methods required it (not UI operations, but calls to shared preferences and content providers). I have now learnt that this approach can lead to undesirable outcomes if the Activity
is destroyed and garbage collected, but it did compile and run fine.
I began this change because I wanted to make my loading screen behave better when the app was paused and stopped. I realised people frown on loading screens in Android but for me it is required as I have an operation that will take 20 seconds or so and that needs to be completed before the app will function.
So using this guide, I began improving my app.
In short the guide moves the AsyncTask
into a Fragment
that does not have an attached UI, with a separate Fragment
for displaying the loading screen with ProgressBar
. This means that the Fragment that spawns the AsyncTask
does not have a Context, meaning I cant pass one in to the AsyncTask
.
As I said before I have operations in the AsyncTask
that require a Context
object, so where can I get it from? Should I just pass in that data to the AsyncTask
before I start?