How to delay Fragment UI creation until after GET request from the server resolves?
<p>My launcher activity is a TabLayout that hosts several of the below Fragments. The vision is that on each Tab click, the Fragment's onCreate will use the Retrofit2 library to GET data from the server and display the data through onCreateView's RecyclerView. </p>

<p>Currently the app crashes on startup because onCreateView finishes first and wants to display the data before the asynchronous GET request even completes. Specifically, the inner class ModelHolder's getItemCount method produces a null object reference. </p>

<p>I've two ideas to delay the Fragment's view creation until after the GET request completes so that the view will actually have data to display.</p>

<p>1) Force synchronicity on the Fragment so that onCreate will have to finish before onCreateView even starts. The app will freeze for a few moments but it won't crash like before. </p>

<p>2) Add a Splash Screen and make it the launcher activity so that the user will be looking at the Splash Screen while the data loads. I think this is cheating because it dodges the problem.</p>

<p>What's the best way to delay onCreateView? If the approach isn't good practice, what alternative solutions are there?</p>

<pre><code>public class ModelsFragment extends Fragment {

private final String API_KEY = "INSERT API KEY HERE";

private static final String ARG_CATEGORY = "model_category";
private String mCategory;

private RecyclerView mModelRecyclerView;
private ModelAdapter mModelAdapter;

private List&lt;Model&gt; mModels;

public static Fragment newInstance(String category) {
    Bundle args = new Bundle();
    args.putString(ARG_CATEGORY, category);
    ModelsFragment modelsFragment = new ModelsFragment();
    modelsFragment.setArguments(args);

    return modelsFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCategory = getArguments().getString(ARG_CATEGORY);

    EdmundsService service = EdmundsServiceGenerator.createService(EdmundsService.class);

    Map&lt;String, String&gt; options = new HashMap&lt;&gt;();
    options.put("state", "new");
    options.put("year", "2016");
    options.put("view", "basic");
    options.put("api_key", API_KEY);
    if (mCategory != null) {
        options.put("category", mCategory);
    }

    Call&lt;Models&gt; call = service.getModels(options);
    call.enqueue(new Callback&lt;Models&gt;() {
        @Override
        public void onResponse(Call&lt;Models&gt; call, Response&lt;Models&gt; response) {
            if (response.isSuccessful()) {
                mModels = response.body().getModels();
                Log.i("GET Status", "Successfully retrieved data");
                Log.i("GET Status", response.body().getModelsCount().toString());
            }
            else {
                Log.i("GET Status", "Failed to retrieve data");
            }
        }

        @Override
        public void onFailure(Call&lt;Models&gt; call, Throwable t) {
            Log.e("Error retrieving data", t.getMessage());
        }
    });
}

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

    mModelRecyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_models, container, false);
    updateUI();
    mModelRecyclerView.setHasFixedSize(true);
    mModelRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    return mModelRecyclerView;
}

private void updateUI() {
    mModelAdapter = new ModelAdapter(mModels);
    mModelRecyclerView.setAdapter(mModelAdapter);
}

public class ModelHolder extends RecyclerView.ViewHolder {
    public ModelHolder(LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.fragment_models_card_view_item, parent, false));

    }
}

public class ModelAdapter extends RecyclerView.Adapter&lt;ModelHolder&gt; {
    // Set number of Cards in the recycler view.
    private List&lt;Model&gt; models;

    public ModelAdapter(List&lt;Model&gt; models) {
        this.models = models;
    }

    @Override
    public ModelHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ModelHolder(LayoutInflater.from(parent.getContext()), parent);
    }

    @Override
    public void onBindViewHolder(ModelHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return models.size();
    }
}

}
</code></pre>