0

I'm creating an android app which has single activity for now. This activity calls an api to get JSON response and populate the Fragments. Once the JSON response is received I am updating the data set and notifying adapter. Currently the JSON request is called in onCreate method.

Similarly in each fragment I need to load image from network so I am wondering will it too should be put in onCreateView or someplace else?

I'm interested in knowing what is the best practice to achieve this.

gk.
  • 338
  • 5
  • 15
  • 1
    As long as you are not blocking the UI thread, it's ok. If your network call blocks the UI thread, you would receive a NetworkOnMainThreadException. In that case, uses [AsyncTask](https://developer.android.com/reference/android/os/AsyncTask) – Ricky Mo Apr 02 '19 at 08:15
  • @RickyMo thanks for suggesting AsyncTask. Actually my fragments rely on data so I am placing a dummy fragment with progress bar initially and when network call finish updates the data set. Not sure if it's correct way to do it. But yes it's not blocking main thread. – gk. Apr 02 '19 at 10:48

2 Answers2

1

The best practise is to use an asynchronous event when doing networking stuff, because depending on your server response time andd on your network speed, you will maybe get the response late and that block your UI and that's not good.

I suggest you integrate AsyncTask when calling your API.

Here is an example

class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {


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

}

@Override
protected Boolean doInBackground(String... urls) {
try {

   //here you make your api call and json parsing

} catch (IOException e) {
    e.printStackTrace();
} catch (JSONException e) {

    e.printStackTrace();
}
return false;
}

protected void onPostExecute(Boolean result) {

}

And you call your request in your onCreate like this

JSONAsyncTask task=new JSONAsyncTask();
task.execute();

Take a look at docs for further information https://developer.android.com/reference/android/os/AsyncTask

Amine
  • 2,241
  • 2
  • 19
  • 41
1

It's fine doing network call in onCreate, put my two cents in i suggest you this good practices that i use.

-Study network call and try to estabilish how many time it takes and if result is needed for UI thread, For long operations AsyncTask should not be used

From documentation

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

-Never reinvent the wheel you could use a lot of libray for network operations like:

  • Retrofit
  • OkHttp
  • Volley
  • AndroidFastNetworking

-Always handle error! There is nothing more annoying than an infinite loader caused by some connection error, check internet connection with BroadCast Receiver