Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,808,356 Members 38,113 Now Online
XDA Developers Android and Mobile Development Forum

[Q] How to prevent constant executing of asynctask

Tip us?
 
lupajz
Old
#1  
Member - OP
Thanks Meter 3
Posts: 38
Join Date: Jan 2011
Default [Q] How to prevent constant executing of asynctask

Basicly I have very same code as described here http://www.learn2crack.com/2013/12/a...t-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
Old
(Last edited by SimplicityApks; 8th January 2014 at 05:33 PM.)
#2  
SimplicityApks's Avatar
Senior Member
Thanks Meter 327
Posts: 323
Join Date: May 2013
Location: Aachen
Quote:
Originally Posted by lupajz View Post
Basicly I have very same code as described here http://www.learn2crack.com/2013/12/a...t-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.
 
lupajz
Old
#3  
Member - OP
Thanks Meter 3
Posts: 38
Join Date: Jan 2011
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.

Quote:
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
Old
#4  
SimplicityApks's Avatar
Senior Member
Thanks Meter 327
Posts: 323
Join Date: May 2013
Location: Aachen
Quote:
Originally Posted by lupajz View Post
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 ).

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

Advanced Search
Display Modes