Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right Approach to Call Web Service(API) from Fragment class

I need to know, In which fragment callback method, we should call a web service by which after come back to fragment web service should not call again.

For example. I have a fragment class MyFragment.java

public class MyFragment extends Fragment {


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_layout, container,
                false);

        return rootView;
    }

}

I need to know which callback method I have to call webservice to update the UI of fragment. Right Now I am calling web services from onCreateView method. but I need to know what should be best way to call web service from fragment.

like image 537
Ashish Dwivedi Avatar asked Apr 28 '14 05:04

Ashish Dwivedi


People also ask

Where do we call API in fragment?

If you app follows SingleActivity-MultipleFragments pattern, then you can call API related stuff in your Activity. You can roam around fragments, use the data fetched by Activity in those fragments, update the data as necessary.

How do you call a Class fragment?

private FragmentManager fragmentManager = getSupportFragmentManager(); private YourFragment yourFragment; if (getIntent(). hasExtra("key")) { FragmentTransaction fragmentTransaction = fragmentManager. beginTransaction(); yourFragment = new YourFragment(); fragmentTransaction.


2 Answers

If I understand your problem correctly, you want to fetch some data from the server and then inform the fragment that data is prepared and redraw the fragment, is that correct? According to the documentation here:

onCreate() - The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

onCreateView() The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

When you create a Fragment somewhere else in your application, onCreate() method is called. When fragment has to be drawn for the first time, onCreateView() is called and this method returns a created View. In your case, you could probably go with something like:

  1. Declare an instance variable (container) for this data and adapter (if you use any).
  2. In onCreate, initialize all this data (empty container), initialize adapter and then execute the AsyncTask.
  3. In onCreateView, prepare the view to return - adapter etc. So now, once AsyncTask will finish, in onPostExecute it calls your_adapter.notifyDataSetChanged(). This will redraw the fragment, since adapter will be informed that data has changed (fetched from the server).
like image 172
tjurczyk Avatar answered Oct 03 '22 02:10

tjurczyk


Depends on when you want to fetch the data. Do you want it every time the app comes to the foreground? Use onResume() Do you want it only when the app starts for the first time? Use onViewCreated(), which gets called after onCreateView is finished.

like image 27
Velu Avatar answered Oct 03 '22 04:10

Velu