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):
http://www.2linessoftware.com/2010/08/03/singletons-and-services-and-shutdowns-oh-my/
and good related answers here:
Singletons vs. Application Context in Android?
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 !
Here are the codes:
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();
}
}
Second activity:
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();
}
}
The Singleton:
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;
}
}