Right Approach To Call Web Service(api) From Fragment Class
Solution 1:
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:
- Declare an instance variable (container) for this data and adapter (if you use any).
- In
onCreate
, initialize all this data (empty container), initialize adapter and then execute theAsyncTask
. - In
onCreateView
, prepare the view to return - adapter etc. So now, onceAsyncTask
will finish, inonPostExecute
it callsyour_adapter.notifyDataSetChanged()
. This will redraw the fragment, since adapter will be informed that data has changed (fetched from the server).
Solution 2:
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.
Solution 3:
onCreate() is tricky.
Put everything that you want to call just once in onCreate() method. For example your API call, list, adapter initialization.
So ideally I would do like this
publicvoidonCreate(Bundle sis) {
getImagesFromServer(); // API Call initialzeLists()
initializeAdapters()
}
publicViewonCreateView(LayoutInflater i, ViewGroup c,Bundle sis) {
recyclerView = findViewById()
setUpAdapters()
return rootView;
}
TRICKY PART
But if you really want onCreate()
to be called once then reuse Fragment. (Don't create a new instance every time).
How to reuse a Fragment?
While Adding for first time use add it to backStack with a KEY like this
currentFragment = FirstFragment.getInstance();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentHolder, currentFragment, "FIRST_KEY")
.addToBackStack("FIRST_KEY")
.commit();
And when you want to add it again get the old instance with the KEY like this
currentFragment = getSupportFragmentManager().findFragmentByTag("FIRST_KEY");
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentHolder, currentFragment, "FIRST_KEY")
.addToBackStack("FIRST_KEY")
.commit();
Demo project:
Here is the link to a demo project which loads a list of images using the Volley library. It has a similar implementation.
Post a Comment for "Right Approach To Call Web Service(api) From Fragment Class"