title: How to know when async task is completely finished in android studio?
body: <p><strong>Solved:</strong> <em>I had a problem with concurrent modification which I thought was due to calling my filter before the task was done, what ended up working for me was adding a call to an outside method in the onPostExecute that copied values to a copyonwritearraylist.</em>  </p>
  
  <p>So I have an AsyncTask that parses json from a website  and initializes a list when its done.</p>
  
  <pre><code>    inner class GetSchoolAsync : AsyncTask&lt;String, String, String&gt;() {
      lateinit var list: MutableList&lt;Info&gt;
      override fun onPreExecute() {
          // Before doInBackground
      }
  
      override fun doInBackground(vararg urls: String?): String {
          var urlConnection: HttpURLConnection? = null
  
          try {
              val url = URL(urls[0])
  
              urlConnection = url.openConnection() as HttpURLConnection
              urlConnection.connectTimeout = CONNECTON_TIMEOUT_MILLISECONDS
              urlConnection.readTimeout = CONNECTON_TIMEOUT_MILLISECONDS
  
              val inString = urlConnection.inputStream.bufferedReader().readText()
  
              publishProgress(inString)
          } catch (ex: Exception) {
              println("HttpURLConnection exception" + ex)
          } finally {
              if (urlConnection != null) {
                  urlConnection.disconnect()
              }
          }
  
          return " "
      }
  
      override fun onProgressUpdate(vararg values: String?) {
          try {
              var list = mutableListOf&lt;Info&gt;()
              var data = Gson().fromJson(values[0], Read::class.java)
  
              for(item in data.schools){
                  list.add(item)
                 // println(item)
              }
              this.list = list
  
  
  
          } catch (ex: Exception) {
              println("JSON parsing exception" + ex.printStackTrace())
          }
      }
  
      override fun onPostExecute(result: String?) {
          // Done
          schoolList = this.list
  
      }
  }
  </code></pre>
  
  <p>It is being executed in the mainactivity OnCreate along with another async function that renders a map. School list is a private lateinit var in the main activity.</p>
  
  <pre><code>    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_maps)
      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
      val mapFragment = supportFragmentManager
              .findFragmentById(R.id.map) as SupportMapFragment
      GetSchoolAsync().execute("https://code.org/schools.json")
      mapFragment.getMapAsync(this)
  }
  </code></pre>
  
  <p>I need to know when the json part is finished and the list is initialized so that I can add markers to the map. Any time I have tried to check the status of the Async task in the debugger its always showing that its either pending or running. When can I be certain that its finished?</p>