1

In which method it will be appropriate to call the web service from an android fragment?

  • oncreateView() or
  • oncreate() or
  • onViewCreated()
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
user3226628
  • 119
  • 1
  • 6
  • hey checks these links: http://stackoverflow.com/questions/21852917/android-fragment-which-life-cycle-method-to-use-for-web-service-call http://stackoverflow.com/questions/23333092/right-approach-to-call-web-serviceapi-from-fragment-class http://stackoverflow.com/questions/21282126/web-service-calls-in-fragment-class and your requirement or problem needs more details. – Tarun Sharma Sep 23 '14 at 06:32
  • 1
    None, efficient implementation will be by involving appropriate Design Patterns into your code, particularly Observer pattern. – Techfist Sep 23 '14 at 06:33

2 Answers2

2

Use onStart() method

  • Use an Async Task call in the onStart() mentod and run a background thread
  • In the AsyncTask use doInBackground() to run the methods that take longer time to execute
  • Update the UI thread in onPreExecute(), onPostExecute(), onProgressUpdate()

Async task Example :

public class FrgLatein extends Fragment {
    //New-Instance
    public static FrgLatein newInstance(){
        Log.d("FrgLatein", "newInstance");
        FrgLatein fragment = new FrgLatein();
        return  fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.d("FrgLatein", "onCreateView");
        View view=inflater.inflate(R.layout.frg_latein, container, false);
        setHasOptionsMenu(true);


        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.d("FrgLatein", "onActivityCreated");
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onStart() {
        Log.d("FrgLatein", "onStart");
        super.onStart();
        new LongOperation().execute("");

    }

private class LongOperation extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
             // Do the Web service long run here
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
          // Do the UI-task here
        }

        @Override
        protected void onPreExecute() {
          // Do the UI-task here
        }

        @Override
        protected void onProgressUpdate(Void... values) {
          // Do the UI-task here which has to be done during backgroung tasks are running like a downloading process
        }
    }

}
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • 5
    Boldness is not required. use it where it is required not all the time. you are always using boldness in your answers which is not actually required. – SilentKiller Sep 23 '14 at 06:42
  • 5
    Still some text are bold. which not required. **Use Bold where it is required.** – SilentKiller Sep 23 '14 at 06:49
-1

You can call the WebService at onCreateView() method of your fragment like this -

public class MainActivity extends Fragment
{

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.mainFragment,container ,false);

    new GetWebService().execute();

    return view;
  }

  class GetWebService extends AsyncTask<String, String, String> 
  {

    @Override
    protected void onPreExecute() 
    {
       super.onPreExecute();
    }

    protected String doInBackground(String... args) 
    {

       //Call Webservice here and return the result

       return "";  
    }

    protected void onPostExecute(String result) 
    {
       //Do something with result
    }
}
sjain
  • 23,126
  • 28
  • 107
  • 185