title: Android AsyncTask re-runs when activity is opened
body: <p>In my App the user has to login.</p>
  
  <ul>
  <li>They open the app on the login page</li>
  <li>They enter email/password and hit login</li>
  <li>A LoadingScreenActivity is opened that has a swirly circle and is running an AsyncTask     that goes to my database and retrieves all the users info</li>
  </ul>
  
  <p>After the AsyncTask is completed it starts an intent to launch MainPageActivity.</p>
  
  <p>There are two problems with this at the moment:</p>
  
  <ol>
  <li>If the user logs in and then goes to the home screen while the app loads the MainPageActivity will open as soon as it is ready (on top of the existing home page) even though the app has been minimised</li>
  <li>If the user logs in and then goes to the home screen while the app loads and then returns to the loading screen the AsyncTask will complete twice over </li>
  </ol>
  
  <p>For problem 1. At the moment my onPostExecute() method in LoadingScreenActivity looks like this:</p>
  
  <pre><code>    @Override
      public void onPostExecute() {
          //open the main page  
          Intent mainPage = new Intent(getApplicationContext(), MainPageActivity.class);                             
          mainPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          mainPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
          startActivity(mainPage);
      }
  </code></pre>
  
  <p>Is there a way I could detect in this method if the main page activity should be opened yet?       </p>
  
  <p>For problem 2. I've hit a complete road block on this, is there a way to detect if the activity has simply been re opened rather than started for the first time? I'd really appreciate any tips on this, I'm quite new to android so I'm not even convinced an Async task is the way to go with this.</p>
  
  <p>Thanks for your time</p>
  
  <p>LoadingScreenActivity.java</p>
  
  <pre><code>    public class LoadingScreenActivity extends Activity implements TaskFragment.TaskCallbacks {
  
  private static final String TAG_TASK_FRAGMENT = "task_fragment";
  private TaskFragment mTaskFragment;
  
  @Override 
  public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
  
      FragmentManager fm = getFragmentManager();
      mTaskFragment = (TaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT);
  
      // If the Fragment is non-null, then it is currently being
      // retained across a configuration change.
      if (mTaskFragment == null) {
        mTaskFragment = new TaskFragment();
        fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit();
      }
  
      setContentView(R.layout.loading_screen);
      ActionBar actionBar = getActionBar();
      actionBar.hide();
      TextView title = (TextView) findViewById(R.id.loading_title);  
      TextView progress = (TextView) findViewById(R.id.loading_progress);  
  
      title.setText(R.string.app_name);
      progress.setText("Loading your info");
  }
  
  
  
  @Override
  public Context onPreExecute() { 
  
      return getApplicationContext();
  }
  
  @Override
  public void onProgressUpdate(int percent) {   
  }
  
  @Override
  public void onCancelled() {
      Intent login = new Intent(getApplicationContext(), LoginActivity.class);
      login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(login);
      finish();
  }
  
  @Override
  public void onPostExecute() {
      //open the main page  
      Intent mainPage = new Intent(getApplicationContext(), MainPageActivity.class);                             
      mainPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      mainPage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
      startActivity(mainPage);
  }
  
  }
  </code></pre>
  
  <p>and TaskFragment.java</p>
  
  <pre><code>public class TaskFragment extends Fragment {
  
  
    static interface TaskCallbacks {
      Context onPreExecute();
      void onProgressUpdate(int percent);
      void onCancelled();
      void onPostExecute();
    }
  
    private TaskCallbacks mCallbacks;
    private DummyTask mTask;
  
  
    @Override
    public void onAttach(Activity activity) {
      super.onAttach(activity);
      mCallbacks = (TaskCallbacks) activity;
  
  
    }
  
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      // Retain this fragment across configuration changes.
      setRetainInstance(true);
  
      // Create and execute the background task.
      mTask = new DummyTask();
      mTask.execute();
    }
  
  
    @Override
    public void onDetach() {
      super.onDetach();
      mCallbacks = null;
    }
  
  
    private class DummyTask extends AsyncTask&lt;Void, Integer, Void&gt; {
    Context context;
    boolean running = true;
  
  @Override
  protected void onPreExecute() {
    if (mCallbacks != null) {
      context = mCallbacks.onPreExecute();
    }
  }
  
  
  @Override
  protected Void doInBackground(Void... ignore) {
      //Get the current thread's token  
      synchronized (this)  
      {
          if(running){
              DatabaseHandler dbHandler = new DatabaseHandler(context);
              dbHandler.populateSQLiteDatabase();
          }
  
      }  
  
      return null;
  }
  
  @Override
  protected void onProgressUpdate(Integer... percent) {
    if (mCallbacks != null) {
      mCallbacks.onProgressUpdate(percent[0]);
    }
  }
  
  @Override
  protected void onCancelled() {
    if (mCallbacks != null) {
      mCallbacks.onCancelled();
    }
  }
  
  @Override
  protected void onPostExecute(Void ignore) {
    if (mCallbacks != null) {
      mCallbacks.onPostExecute();
    }
  }
  
  
    }
  }
  </code></pre>