@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mRootView = inflater.inflate(R.layout.fragment_builder_activity_cocktails, container, false); initalLoadDone = false; mCocktailList = (ListView) mRootView.findViewById(R.id.lvBuilderCocktails); mNoCocktails = (TextView) mRootView.findViewById(R.id.tvBuilderNoDataView); cocktailIngredients = Utilities.loadSavedIngredientList(getActivity()); return mRootView; }
// Reference: // http://stackoverflow.com/questions/9323279/how-to-test-if-a-fragment-view-is-visible-to-the-user @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); isFragmentVisible = isVisibleToUser; if (isFragmentVisible) { Log.d( LOG_TAG, "Total ingredients: " + Integer.toString(Utilities.loadSavedIngredientList(getActivity()).size())); if (Utilities.loadSavedIngredientList(getActivity()).size() == 0) { mCocktailList.setVisibility(View.GONE); mNoCocktails.setVisibility(View.VISIBLE); } else { // If it has not changed size if (Utilities.loadSavedIngredientList(getActivity()).size() == cocktailIngredients.size()) { // Unless the inital load has been completed if (initalLoadDone == false) { if (Utilities.isNetworkAvailable(getActivity())) { new LoadCocktails().execute(); } else { Log.e(LOG_TAG, "No internet connection"); Toast.makeText(getActivity(), R.string.no_internet, Toast.LENGTH_LONG).show(); } initalLoadDone = true; } } else { // List has been changed so update if (Utilities.isNetworkAvailable(getActivity())) { new LoadCocktails().execute(); } else { Log.e(LOG_TAG, "No internet connection"); Toast.makeText(getActivity(), R.string.no_internet, Toast.LENGTH_LONG).show(); } cocktailIngredients = Utilities.loadSavedIngredientList(getActivity()); } mCocktailList.setVisibility(View.VISIBLE); mNoCocktails.setVisibility(View.GONE); } } }