I am getting null while calling getSherlockActivity();
This is because the Activity is finished or killed
I could not come up with a solution to avoid in my case:
This SherlockActivity has a ViewPager with 4 Tabs.
adapter = new PagerAdapter((SherlockFragmentActivity) this, mViewPager);
adapter.addTab(actionBar.newTab(), Frag1.class, bundle);
adapter.addTab(actionBar.newTab(), Frag2.class, bundle);
adapter.addTab(actionBar.newTab(), Frag3.class, bundle);
adapter.addTab(actionBar.newTab(), Frag4.class, bundle);
When this Activity is launched, all the Tabs(Fragments) have an AsyncTask which uses getSherlockActivity() in doInBackground.
So, for all the AsyncTasks to complete, it takes around 3-5 secs, meanwhile if i press back and finish this Activity, the Last fragment Frag4 which didn’t yet started its AsyncTask(might be queued) tries for getSherlockActivity() and gets crashed.
I could just use a Try-Catch block and solve this, but is there a way i can tell the Fragments If the parent Activity is dead, stop all your background tasks etc.. something like that?
Thank You
Just call each fragment’s AsyncTask.cancel() in the Activity onDestroy() callback. That way, the asynctasks aren’t going to perform anything based on the destroyed Activity context.