title: Where should i add my method for noInternetConnection? Android
body: <p>It is all working and i have added my method to check if there is internet connection, and if there isn't change activity. But it is changing activity immediately before loading progress bar. Where should i add this method so first progress bar is loaded and if there is no internet connection, change activity. </p>
  
  <p>Also if anyone have some suggestion about this checking internet connection, i would appricieate any answer.</p>
  
  <p><strong>This is my activity where i'm using that method:</strong></p>
  
  <pre><code>public class ListaPreporuka extends AppCompatActivity {
  // Log tag
  private static final String TAG = ListaPreporuka.class.getSimpleName();
  
  // Movies json url
  private static final String url = "http://www.nadji-ekipu.org/wp-content/uploads/2015/07/movies.txt";
  private ProgressDialog pDialog;
  private List&lt;Movie&gt; movieList = new ArrayList&lt;Movie&gt;();
  private ListView listView;
  private CustomListAdapter adapter;
  private static String Title ="title";
  private static String bitmap ="thumbnailUrl";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.lista_preporuka);
  
      getSupportActionBar().setDisplayShowHomeEnabled(true);   
      getSupportActionBar().setHomeButtonEnabled(true);
      getSupportActionBar().setDisplayUseLogoEnabled(true);
      getSupportActionBar().setLogo(R.drawable.ic_horor_filmovi_ikonica);
  
      Intent newActivity2=new Intent();
      setResult(RESULT_OK, newActivity2);
  
      listView = (ListView) findViewById(R.id.list);
      adapter = new CustomListAdapter(this, movieList);
      listView.setAdapter(adapter);
  
      pDialog = new ProgressDialog(this);
      // Showing progress dialog before making http request
      pDialog.setMessage("Učitavanje...");
      pDialog.show();      
  
      // Creating volley request obj
      JsonArrayRequest movieReq = new JsonArrayRequest(url,
              new Response.Listener&lt;JSONArray&gt;() {
                  @Override
                  public void onResponse(JSONArray response) {
                      Log.d(TAG, response.toString());
                      hidePDialog();
  
                      // Parsing json
                      for (int i = 0; i &lt; response.length(); i++) {
                          try {
  
                              JSONObject obj = response.getJSONObject(i);
                              Movie movie = new Movie();
                              movie.setTitle(obj.getString("title"));
                              movie.setThumbnailUrl(obj.getString("image"));
                              movie.setRating(((Number) obj.get("rating"))
                                      .doubleValue());
                              movie.setYear(obj.getInt("releaseYear"));
  
                              // Genre is json array
                              final JSONArray genreArry = obj.getJSONArray("genre");
                              ArrayList&lt;String&gt; genre = new ArrayList&lt;String&gt;();
                              for (int j = 0; j &lt; genreArry.length(); j++) {
                                  genre.add((String) genreArry.get(j));
                              }
                              movie.setGenre(genre);
  
                              // adding movie to movies array
                              movieList.add(movie);
  
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }                        
  
                      }
  
                      // notifying list adapter about data changes
                      // so that it renders the list view with updated data
                      adapter.notifyDataSetChanged();
                  }
              }, new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      VolleyLog.d(TAG, "Error: " + error.getMessage());
                      hidePDialog();
  
                  }
              });
  
      noInternet();
  
      // Adding request to request queue
      AppController.getInstance().addToRequestQueue(movieReq);
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                                  int position, long id) { 
              String name = ((TextView) view.findViewById(R.id.title))
                      .getText().toString();
  
              bitmap = ((Movie)movieList.get(position)).getThumbnailUrl();
  
              Intent intent = new Intent(ListaPreporuka.this, MoviesSingleActivity.class);    
              intent.putExtra(Title, name);
              intent.putExtra("images", bitmap);
              intent.putExtra("Year", movieList.get(position).getYear());             
              startActivity(intent);
              overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
              }
          });
  
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      hidePDialog();
  }
  
  private void hidePDialog() {
      if (pDialog != null) {
          pDialog.dismiss();
          pDialog = null;
      }
  }
  
  //WHERE SHOULD I ADD THIS METHOD FOR MAKING MY ACTIVITY SHOW FIRST PROGRESS BAR AND IF THERE IS NO INTERNET CONNECTION, SHOW ANOTHER ACTIVITY
  private void noInternet() {
      ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo info = cm.getActiveNetworkInfo();
      if (info != null) {
          if (!info.isConnected()) {
          }
      }
      else {
          Toast.makeText(this, "No Internet Connection", Toast.LENGTH_SHORT).show();
          Intent intent = new Intent(ListaPreporuka.this, NoConnection.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(intent);
          overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
      }
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      if (item.getItemId() == android.R.id.home) {
          finish();
          overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
          return true;
      }
      return false;
  }
  
  @Override
  public void onBackPressed() {
  
      super.onBackPressed();
      overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
  }
  </code></pre>