title: How to refresh fragment on spinner onItemSelected in android
body: <p>I have a fragment with a spinner and a listview the list view is updated when the user chooses one of the options from the spinner, and I came to an idea that I have to refresh the fragment when an option is chosen. but the issue is the fragment goes in an infinity loop when I refresh the fragment from &quot;onItemSelected&quot; so I made a &quot;refreshFragment method&quot; to solve the issue but now after the user chooses an option the first option from the spinner don't refresh the fragment again, all the other options working well except the first one because I didn't add the &quot;refreshFragment method&quot; in to this option because if I add it it will go to the infinity loop again.</p>
  <pre><code>    public class MatchesFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemSelectedListener {
      
          private RecyclerView recyclerView;
          private MatchAdapter matchAdapter;
          private ArrayList&lt;Matches&gt; matchCollection;
          SwipeRefreshLayout mSwipeRefreshLayout;
      
          String apiLink;
          String apiType = &quot;1&quot;;
      
      
          public MatchesFragment() {
              // Required empty public constructor
          }
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
          }
      
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
              // Inflate the layout for this fragment
              return inflater.inflate(R.layout.fragment_matches, container, false);
          }
      
          @Override
          public void onActivityCreated(@Nullable Bundle savedInstanceState) {
              super.onActivityCreated(savedInstanceState);
      
              mSwipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swiperefresh);
              mSwipeRefreshLayout.setOnRefreshListener(this);
      
      
              if (getArguments() != null) {
                  Bundle arguments = getArguments();
                  apiLink = arguments.getString(&quot;ApiLink&quot;);
                  apiType = arguments.getString(&quot;apiType&quot;);
                  Toast.makeText(getContext().getApplicationContext(), apiType, Toast.LENGTH_LONG).show();
              }else {
                  apiLink = &quot;http://first-api-link&quot;;
                  Toast.makeText(getContext().getApplicationContext(), &quot;nothing&quot;, Toast.LENGTH_LONG).show();
              }
      
              Spinner spinner = (Spinner) getView().findViewById(R.id.spinner);
              spinner.setOnItemSelectedListener(this);
      
      // Create an ArrayAdapter using the string array and a default spinner layout
              ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(getActivity(),
                      R.array.planets_array, android.R.layout.simple_spinner_item);
      // Specify the layout to use when the list of choices appears
              adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      // Apply the adapter to the spinner
              spinner.setAdapter(adapter);
      
              ConnectivityManager cm = (ConnectivityManager) getContext().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
      
              NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
              String answer = &quot;Internet&quot;;
              if (null != activeNetwork) {
                  if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                      answer=&quot;You are connected to a WiFi Network&quot;;
                  if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                      answer=&quot;You are connected to a Mobile Network&quot;;
                  init();
                  new FetchDataTask().execute();
              }
              else {
                  answer = &quot;No internet Connectivity&quot;;
                  Toast.makeText(getContext().getApplicationContext(), answer, Toast.LENGTH_LONG).show();
              }
          }
      
          private void init() {
              recyclerView = (RecyclerView) getActivity().findViewById(R.id.matchesRecyclerview);
              recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
              recyclerView.setHasFixedSize(true);
              matchCollection = new ArrayList&lt;&gt;();
              matchAdapter = new MatchAdapter(matchCollection, getActivity());
              recyclerView.setAdapter(matchAdapter);
          }
      
          @Override
          public void onRefresh() {
              init();
              new FetchDataTask().execute();
          }
      
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
      
              apiLink = &quot;http://first-api-link&quot;;
              switch (position){
  //can't add refreshFragment in here it makes infinity lopp
                  case 0: apiLink = &quot;first-api-link&quot;;
                  break;
                  case 1:
                      refreshFragment(&quot;second-api-link&quot;, 2);
                  break;
                  case 2:
                      refreshFragment(&quot;third-api-link&quot;, 3);
                      break;
                  case 3:
                      refreshFragment(&quot;fourth-api-link&quot;, 4);
                      break;
                  case 4:
                      refreshFragment(&quot;fifth-api-link&quot;, 5);
                      break;
                  case 5:
                      refreshFragment(&quot;sixth-api-link&quot;, 6);
                      break;
              }
          }
      
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; parent) {
      
          }
      
          public class FetchDataTask extends AsyncTask&lt;Void, Void, Void&gt; {
              private String mZomatoString;
      
              @Override
              protected Void doInBackground(Void... params) {
                  HttpURLConnection urlConnection = null;
                  BufferedReader reader = null;
                  Uri builtUri = Uri.parse(apiLink);
                  URL url;
                  try {
                      url = new URL(builtUri.toString());
                      urlConnection = (HttpURLConnection) url.openConnection();
                      urlConnection.setRequestMethod(&quot;GET&quot;);
                      urlConnection.setRequestProperty(&quot;user-key&quot;, &quot;acfd3e623c5f01289bd87aaaff1926c1&quot;);
                      urlConnection.connect();
      
                      InputStream inputStream = urlConnection.getInputStream();
                      StringBuffer buffer = new StringBuffer();
                      if (inputStream == null) {
                          //Nothing to do
                          return null;
                      }
      
                      reader = new BufferedReader(new InputStreamReader(inputStream));
      
                      String line;
                      while ((line = reader.readLine()) != null) {
                          buffer.append(line + &quot;\n&quot;);
                      }
      
                      if (buffer.length() == 0) {
                          return null;
                      }
      
                      mZomatoString = buffer.toString();
                      JSONObject jsonObject = new JSONObject(mZomatoString);
      
                      Log.v(&quot;Response&quot;, jsonObject.toString());
      
                      JSONArray matchArray = jsonObject.getJSONArray(&quot;data&quot;);
      
      
                      //list = new ArrayList&lt;&gt;();
                      for (int i = 0; i &lt; matchArray.length(); i++) {
      
                          Log.v(&quot;BRAD_&quot;, i + &quot;&quot;);
                          String home_team;
                          String away_team;
                          String home_flag;
                          String away_flag;
      
                          JSONObject jTeam = (JSONObject) matchArray.get(i);
      
                          if (apiType == &quot;1&quot;){
                          JSONObject jHomeTeam = (JSONObject) jTeam.getJSONObject(&quot;home_team&quot;);
                          JSONObject jAwayTeam = (JSONObject) jTeam.getJSONObject(&quot;away_team&quot;);
      
                          //get data's from json sent from link
                          home_team = jHomeTeam.getString(&quot;name&quot;);
                          away_team = jAwayTeam.getString(&quot;name&quot;);
                          home_flag = jHomeTeam.getString(&quot;flag&quot;);
                          away_flag = jAwayTeam.getString(&quot;flag&quot;);
                          }else{
                              //get data's from json sent from link
                              home_team = jTeam.getString(&quot;home_team&quot;);
                              away_team = jTeam.getString(&quot;away_team&quot;);
                              home_flag = &quot;https://www.countries-ofthe-world.com/flags-normal/flag-of-Russia.png&quot;;
                              away_flag = &quot;https://www.countries-ofthe-world.com/flags-normal/flag-of-Russia.png&quot;;
                          }
      
      
      
                          Matches matches = new Matches();
                          matches.setHome_team(home_team);
                          matches.setAway_team(away_team);
                          matches.setHome_flag(home_flag);
                          matches.setAway_flag(away_flag);
      
                          matchCollection.add(matches);
                          // Stopping swipe refresh
                          mSwipeRefreshLayout.setRefreshing(false);
                      }
                  } catch (MalformedURLException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (JSONException e) {
                      e.printStackTrace();
                  } finally {
                      if (urlConnection != null) {
                          urlConnection.disconnect();
                      }
                      if (reader != null) {
                          try {
                              reader.close();
                          } catch (final IOException e) {
                              Log.e(&quot;MainActivity&quot;, &quot;Error closing stream&quot;, e);
                          }
                      }
                  }
                  return null;
              }
      
              @Override
              protected void onPostExecute(Void aVoid) {
                  matchAdapter.notifyDataSetChanged();
              }
          }
      
          public void refreshFragment(String apiLinks, int apiType){
              FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
              FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
              MatchesFragment fragment = new MatchesFragment();
              Bundle args = new Bundle();
              args.putString(&quot;ApiLink&quot;, apiLinks);
              args.putString(&quot;apiType&quot;, String.valueOf(apiType));
              fragment.setArguments(args);
              fragmentTransaction.replace(R.id.linearfr, fragment);
              fragmentTransaction.commit();
          }
      
      }
  </code></pre>