Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

AsyncTask Background Processing in Fragments

Hello,

At the moment, my application is based on a fragmentActivity, in which it hosts 4 other fragments which we can access them in through the action bar.

Each Fragment has an Asynctask to retrieve information for every fragment. (i don't know if this is the best way to do it)

However, everytime i'm on a fragment, and i want to go to another section i need to wait for it to load the list completely otherwise the application will crash giving me NullPointerException.

So my questions are:

1 - Do i do 1 asynctask that will update all fragments? 2 - After the list has been loaded, how can i keep the information from it, and not have the AsyncTask run again.

Thanks in Advance, Ayman

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi Ayman,

For question 1, without looking at your app or code, I would guess that you would want to make sure you run your AsyncTask for whichever fragment is visible right away, and then run the others in the background to update the other fragments while the user is free to use the first fragment.

For question 2, where is your list, and where are the AsyncTasks running? You might be able to run and store things in such a way that they only run once, or maybe you would need to set a flag that you can check before running the AsyncTask. It's hard to say without looking at the code. :)

Hopefully this helps!

Hi Ben,

For (for question 1) Yes Exactly.

For (for question 2) At the moment, on each fragment (they all share the same layout.xml) there is a list and an AsyncTask. Data are still not stored anywhere, (which i might use like a small database to store the last layout and views). You mind checking my code or parts of it to make sure?

Many Thanks, Ayman

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Sorry for the late reply - this totally slipped by me! Did you get it working, or would you like me to take a look at your code? As an independent project I can't promise a solution, but if you email it to me through help@teamtreehouse.com then I can see if anything jumps out.

Hi Ben,

For the 1st question, i managed to work around this. Added a splash Screen at the beginning of the app, that calls the 1st Fragment's Asyntask. After the splash screen, it takes me to my fragment. However the other 3 fragment are still empty, to make them start their asyntask, i need to hover over each fragment for it to start loading. How can i call their asyntask from the 1st fragment.

For question 2: i fixed it.

Best, Ayman