(Android)How to request the network data in the Fragment +Viewpager Mode

942 Views Asked by At

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

There are 1 best solutions below

5
On

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();
           }
    }
}