title: Stop/Resume activities and onCreate in Android
body: <p>I'm a new Android developer and I have a question. After users login, I need to get some data from an external <code>URL</code> and display them on one of my activities. But I've been reading and found this:</p>
  
  <blockquote>
    <p>When your activity comes back to the foreground from the stopped state, it receives a call to onRestart(). The system also calls the
    onStart() method, which happens every time your activity becomes
    visible (whether being restarted or created for the first time).</p>
  </blockquote>
  
  <p>And this</p>
  
  <blockquote>
    <p>Caution: Your activity will be destroyed and recreated each time the
    user rotates the screen. When the screen changes orientation, the
    system destroys and recreates the foreground activity because the
    screen configuration has changed and your activity might need to load
    alternative resources (such as the layout).</p>
  </blockquote>
  
  <p><a href="http://developer.android.com/training/basics/activity-lifecycle/stopping.html" rel="nofollow">Here</a> and <a href="http://developer.android.com/training/basics/activity-lifecycle/recreating.html" rel="nofollow">here</a>.</p>
  
  <p>So, it looks like I should not get the data I need on the <code>onCreate</code> method. Then where? In a previous activity and saving the data in the phone memory? That doesn't sound good to me.</p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <p>edit</p>
  
  <p>I'm using <code>AsyncTask</code> as suggested, but everytime I switch the phone orientation, <code>onCreate</code> method is called.</p>
  
  <p>My MainActivity:</p>
  
  <pre><code>public class MainActivity extends Activity implements AsyncResponse {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          ReadJSONTask jsonTask = new ReadJSONTask();
          jsonTask.delegate = this;
          jsonTask.execute("http://www.myweb.com/myscript.php");
      }
  
      @Override
      public void processFinish(String output) {
          Toast.makeText(getApplicationContext(), output, Toast.LENGTH_LONG).show();
      }
  }
  </code></pre>
  
  <p>ReadJSONTask class:</p>
  
  <pre><code>public class ReadJSONTask extends AsyncTask&lt;String, Void, String&gt; {
      public AsyncResponse delegate = null;
      public String res;
      public Boolean finish = false;
  
      @Override
      protected String doInBackground(String... url) {
          String response = null;
          String adres = url[0];
  
          URL url_HTTPRequest = null;
          try {
              url_HTTPRequest = new URL(adres);
              response = transfer(url_HTTPRequest);
  
          } catch (MalformedURLException e) {
              Log.e("URL ERROR", "MalformedURLException");
          } catch (Exception e) {
              Log.e("URL ERROR", "exc");
          }
  
          return response;
      }
  
      @Override
      protected void onPostExecute(String result) {
          Log.d("mylog", "result= " + result);
          delegate.processFinish(result);
      }
  
      public String transfer(URL url) throws IOException {
  
          HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
          // inputStream = url.openStream();
          InputStream inputStream = urlConnection.getInputStream();
          BufferedReader bin = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
          String line = bin.readLine();
          StringBuffer sb = new StringBuffer();
          while (line != null) {
              sb.append(line);
              sb.append("\r\n");
              line = bin.readLine();
          }
          inputStream.close();
          return sb.toString();
      }
  }
  </code></pre>
  
  <p>And the interface is just:</p>
  
  <pre><code>public interface AsyncResponse {
      void processFinish(String output);
  }
  </code></pre>
  
  <p>As I said, everytime I switch the phone orientation all the async process is performed (and the <code>Toast</code> shows up). That's exactly what I wanted to avoid in the first place.</p>