Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen would Activity's instance die?
    primarykey
    data
    text
    <p>Here is a sample code which make me a little missing:</p> <pre><code>package com.leak; import android.app.Activity; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; public class WindowLeakActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new LeakThread().execute(); } class LeakThread extends AsyncTask&lt;Void, Void,Void&gt;{ ProgressDialog dialog; @Override protected void onPreExecute() { dialog=new ProgressDialog(WindowLeakActivity.this); dialog.show(); } @Override protected Void doInBackground(Void... params) { try { Thread.sleep(2000); finish(); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { //that would be ok if(WindowLeakActivity.this!=null &amp;&amp; !WindowLeakActivity.this.isFinishing()) dialog.dismiss(); } }} </code></pre> <p>As you see,I create a LeakThread and finish the WindowLeakActivity at doInBackground()method.</p> <p>In order to prevent window leak error,I have to check if the Activity has been finished at onPostExecute()method.That make me a little missing.I have following questions:</p> <blockquote> <ol> <li>Is do Activity instance isFinish() check at onPostExecute safe?If my Thread class is not a inner class of Activity.Do I have to check Activity instance is not null at first?</li> <li>When would Activity instance die?As Activity's lifecycle description,it will terminal when callback call onDestroy().But however,the Activity's Thread is still going.Though it's window been not visible,I can also get it's instance.</li> <li>If I call the System.gc().Will it collect Activity's instance?</li> </ol> </blockquote> <p>Sorry for my bad description.Thank you for reading my question very much.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload