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.
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.
private FragmentManager fragmentManager = getSupportFragmentManager(); private YourFragment yourFragment; if (getIntent(). hasExtra("key")) { FragmentTransaction fragmentTransaction = fragmentManager. beginTransaction(); yourFragment = new YourFragment(); fragmentTransaction.
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:
onCreate
, initialize all this data (empty container), initialize adapter and then execute the AsyncTask
.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).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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With