I have called an AsyncTask
inside my fragment
. I was calling getActivity()
from within doInBackground
of that AsyncTask
, but getActivity
is returning null
.
If I call getActivity
outside from AsyncTask
its working properly, but I need instance of activity
inside my asyncTask
itself.
Any help would be appreciated.. !!!
Both Solution will work that i am posting. Try any of them.
Solution 1 :
Try to call your
AsynTask
inonActivityCreated
rather then inonCreateView
as sometimes it happens Activity instance return to benull
inonCreateView
.Solution 2 :
Get
activity
instance whenfragment
is attached onActivity
and then useActivity
context
in yourAsynTask
(No need to call getActivity()).In your fragment add
then use
mActivity
instead ofgetActivity()
in yourAsyncTask
.Pass Activity to AsyncTask method
It may be possible that async task is going on but fragment closed so u have pass getActivty() to AsyncTask.