[Q] How to prevent constant executing of asynctask

Search This thread

lupajz

Member
Jan 27, 2011
39
3
Basicly I have very same code as described here http://www.learn2crack.com/2013/12/android-swipe-view-tab-layout-example.html:

In each of the fragments I have different asynctask, that just fetches data from website. Fragment class looks like this :

Code:
TextView text;
String content;
View today;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    today = inflater.inflate(R.layout.today, container, false);
    text = (TextView) today.findViewById(R.id.textView);
    // ((TextView) today.findViewById(R.id.textView)).setText("Today");

    new RetriveSiteData().execute("http://menza.lupajz.eu/?den=dnes");

    content = (String) text.getText();

    return today;

    <here is the asynctask, too long > 
}

I am wondering if the data is always fetched, while swiping through fragments if the TabPagerAdapter class (on the website) has this method implemented the same way

Code:
@Override
public Fragment getItem(int i) {
    switch (i) {
    case 0:
        //Fragement for Android Tab
        return new Android();
    case 1:
       //Fragment for Ios Tab
        return new Ios();
    case 2:
        //Fragment for Windows Tab
        return new Windows();
    }
    return null;
}

In case if the data is always fetched and the asynctask is always executed how can I prevent that ? Maybe adding some onResume() methods to each fragment ?

What is the best solution just to make connection to website, fetch data once and then just keep displaying them while I am swiping between fragments ?
 

SimplicityApks

Senior Member
May 26, 2013
354
344
Aachen
Basicly I have very same code as described here http://www.learn2crack.com/2013/12/android-swipe-view-tab-layout-example.html:

In each of the fragments I have different asynctask, that just fetches data from website. Fragment class looks like this :

Code:
TextView text;
String content;
View today;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    today = inflater.inflate(R.layout.today, container, false);
    text = (TextView) today.findViewById(R.id.textView);
    // ((TextView) today.findViewById(R.id.textView)).setText("Today");

    new RetriveSiteData().execute("http://menza.lupajz.eu/?den=dnes");

    content = (String) text.getText();

    return today;

     
}

I am wondering if the data is always fetched, while swiping through fragments if the TabPagerAdapter class (on the website) has this method implemented the same way

Code:
@Override
public Fragment getItem(int i) {
    switch (i) {
    case 0:
        //Fragement for Android Tab
        return new Android();
    case 1:
       //Fragment for Ios Tab
        return new Ios();
    case 2:
        //Fragment for Windows Tab
        return new Windows();
    }
    return null;
}

In case if the data is always fetched and the asynctask is always executed how can I prevent that ? Maybe adding some onResume() methods to each fragment ?

What is the best solution just to make connection to website, fetch data once and then just keep displaying them while I am swiping between fragments ?

Yes, that data is always fetched since onCreateView is always called when a Fragment which hasn't been in layout appears, whether it was already instantiated or not. Have a look at the diagram on the right here for better understanding on how the specific methods are called. The dumb solution would be to put that code into the constructor, but Google suggests not doing anything there and instead using onCreate, which is also only called once.

Instead it might be better to just fire up the tasks for each Fragment (maybe sequentially as well) in the Activity, but that depends on how much data you are loading here, since the user might be switched to the page you are loading at the very end.
 
Last edited:

lupajz

Member
Jan 27, 2011
39
3
Thanks for answear yea I was looking at the fragment google site and I guessed that was the problem, but I am not getting the second part.

Instead it might be better to just fire up the tasks for each Fragment (maybe sequentially as well) in the Activity, but that depends on how much data you are loading here, since the user might be switched to the page you are loading at the very end.

I am just getting about 30+ lines of pure text, so it is not that big.
 

SimplicityApks

Senior Member
May 26, 2013
354
344
Aachen
Thanks for answear yea I was looking at the fragment google site and I guessed that was the problem, but I am not getting the second part.

I am just getting about 30+ lines of pure text, so it is not that big.

What I meant was that if you need to load much data over a possibly slow connection and do it in a specific order (meaning start with one category, then when that's finished the next one and so on) it might be that the user is swiping to the last category that will load and has to wait untill all other have been loaded. This is what speaks against loading everything in the activity and then reloading the Fragments with their specific content, but of course this is not the case with just a few lines of text ;). That would be the the way I would do it (load everything in the onCreate() of the activity and pass it to the Fragments) if there is little to medium data to load since you don't have to open an internet connection everytime the user swipes thus swiping will be much smoother (or it won't show a ProgressBar like that stupid xda-app :p).

If it is data that you don't need to reload more than once every day, you can think about cashing it as well!