Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>using your answer as a hint, I've searched over again, and found some good stuff on the internet, it's here (about Singleton-Activity-Service lifecycle):</p> <p><a href="http://www.2linessoftware.com/2010/08/03/singletons-and-services-and-shutdowns-oh-my/" rel="nofollow noreferrer">http://www.2linessoftware.com/2010/08/03/singletons-and-services-and-shutdowns-oh-my/</a></p> <p>and good related answers here:</p> <p><a href="https://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android">Singletons vs. Application Context in Android?</a></p> <p>I've been playing around with Singleton, and find out, that the answers are not quite correct. I tried to create singleton S in an activity A, and then goto activity B (before leaving A, I called A.finish() , and call System.gc() in B.onCreate() ) - but the S.finalize() is not called ! </p> <p>Here are the codes:</p> <pre><code>public class FirstAct extends Activity implements OnClickListener { public static final String TAG = "Test_Service"; private View btChange; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.gc(); setContentView(R.layout.first_lay); btChange = findViewById(R.id.btChange); btChange.setOnClickListener(this); SomeSingleton.getInstance(); } @Override public void onClick(View v) { Intent i = new Intent(this,SecAct.class); startActivity(i); this.finish(); } @Override protected void onDestroy() { Log.i(TAG,"First activity is destroyed !"); super.onDestroy(); System.gc(); } } </code></pre> <p>Second activity:</p> <pre><code>public class SecAct extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.gc(); setContentView(R.layout.first_lay); ((TextView) findViewById(R.id.tvLabel)).setText("Second activity"); findViewById(R.id.btChange).setOnClickListener(this); } @Override public void onClick(View v) { Intent i = new Intent(this,FirstAct.class); this.startActivity(i); this.finish(); } @Override protected void onDestroy() { Log.i(FirstAct.TAG,"Second activity is destroyed !"); super.onDestroy(); System.gc(); } } </code></pre> <p>The Singleton:</p> <pre><code>public class SomeSingleton { private static volatile SomeSingleton instance = null; private static int count = 0; private SomeSingleton() { count++; Log.i(FirstAct.TAG, "Created, instance: #" + count); } public static SomeSingleton getInstance() { if ( instance == null ) { instance = new SomeSingleton(); } return instance; } @Override protected void finalize() throws Throwable { Log.i(FirstAct.TAG, "Finalized! "); super.finalize(); } public void kill() { instance = null; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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