0

In the ViewPager+Fragment by FragmentPagerAdapter mode. I want to known How to load data for Internet.

I want to implement:

1.Only select the current pager can to load data

2.Only performing the load data once to every pager

I search the some Answers: userd the callBack method,I see the lifecyle. It Callback after onCreate. After that dont callBack. if load data so fast . onCreateVie and onActivityCreate don't callBack. View not find. so this method not improper.

boolean isFist
public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isFist&&isVisibleToUser){
         // int this load data

        }
}

so,who can give me good Solutions? Please Forgive my broken English.

1 Answers1

0

This example, shows how to use AsyncTask:

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

So:

  • onPreExecute - Runs on UI Thread. (do nothing, or put a progress dialog)
  • doInBackground - Runs on Background Thread. (download data here)
  • onPostExecute - Runs on UI Thread, recevies the doInBackground results. (show/update data here)

Note: Right now I do not have time to put this example in the context of your specific question, but AsyncTask is your best option.

EXAMPLE:

public class MyActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        new LoadJsonTask().execute("http://example.com/data.json");     
    }
    
    private class LoadJsonTask extends AsyncTask<String, Integer, String > {
           ProgressDialog dialog ;
           protected void onPreExecute (){
                // this happens on UI Thread
                // dialog, of course, is optional.
                dialog = ProgressDialog.show(getActivity() ,"Wait a moment", "Downloading...");
           }
           
           protected String doInBackground (String... params){
               // this happens on background...

               InputStream input = null;
               HttpURLConnection connection = null;
               try {
                   URL url = new URL(params[0]);
                   connection = (HttpURLConnection) url.openConnection();
                   connection.connect();

                   // expect HTTP 200 OK
                   if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                       return null;
                   }

                   // download the file
                   input = connection.getInputStream();
                   BufferedReader r = new BufferedReader(new InputStreamReader(input));
                   StringBuilder result = new StringBuilder();
                   String line;
                   while ((line = r.readLine()) != null) {
                       result.append(line);
                   }
                   return result.toString();
               } catch (Exception e) {
                   // handle exceptions :p
                   return null;
               } finally {
                   // cleanup here                
               }           
           }
           
           protected void onPostExecute(String results){               
                // Do something with results
                dialog.dismiss();
           }
    }
}
Community
  • 1
  • 1
rnrneverdies
  • 15,243
  • 9
  • 65
  • 95
  • Give an irrelevant answer. I want know How to Asynchronous load inside Fragment. you can see my Demand. – lingcong xiao Nov 24 '14 at 06:30
  • You are mistaken, I put a trivial example, or perhaps you expected to do your job? I understood your problem and gave the solution, if you know not appreciate it, okay for me, I do not care who is ungrateful. Please note that your question is very broad and shows no example that has tried. – rnrneverdies Nov 24 '14 at 15:15
  • Inside a Fragment vs Inside an Acitivity => just the same issue. – rnrneverdies Nov 24 '14 at 15:18
  • I want to achieve the effect of (Google)play Newsstand.Dynamic load and only load once – lingcong xiao Nov 25 '14 at 01:29
  • @lingcongxiao edit the post, add a minimal example of your fragment, avoid superficial code. Letme some time, is 23:00hs here right now. – rnrneverdies Nov 25 '14 at 01:36