0

I want to study the Acitvity liftTimes ,then I have two Activities,called MainActivity and SecondActivity,and I set the launch mode to singleTask mode,the code below:

public class MainActivity extends Activity {

/** Called when the activity is first created. */
public static final String PREFS_NAME = "MyPrefsFile";
private TextView tv;
private Button bt;
private final static String TAG = "MainActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e(TAG+"onCreate", "onCreate");
    setContentView(R.layout.activity_main);
    findViews();
    tv.setText("MainActivity ID:" + this.toString());
    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainActivity.this,
                    SecondActivity.class);
            startActivity(intent);

        }
    });
}

private void findViews() {
    // TODO Auto-generated method stub
    tv = (TextView) findViewById(R.id.textView);
    bt = (Button) findViewById(R.id.turnBt);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    Log.e(TAG+"onRestoreInstanceState", "onRestoreInstanceState");
}
@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    Log.e(TAG+"onRestart", "onRestart");
}
@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    Log.e(TAG+"onStart", "onStart");
}
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.e(TAG+"onResume", "onResume");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    Log.e(TAG+"onSaveInstanceState", "onSaveInstanceState");
}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.e(TAG+"onPause", "onPause");
}
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Log.e(TAG+"onDestroy", "onDestroy");
}
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    Log.e(TAG+"onStop", "onStop");
}

}

public class SecondActivity extends Activity{
private TextView tv;
private Button bt;
private final static String TAG = "SECONDACTIVITY";
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Log.e(TAG, "onCreate");
    setContentView(R.layout.second_layout);
    findViews();
    tv.setText("第二个界面的ID:"+this.toString()); 
    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
             Intent intent = new Intent(SecondActivity.this, MainActivity.class);  
                startActivity(intent); 

        }
    });
}

private void findViews() {
    // TODO Auto-generated method stub
    tv = (TextView)findViewById(R.id.textView);
    bt = (Button) findViewById(R.id.turnBt);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    Log.e(TAG, "onRestoreInstanceState");
}
@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    Log.e(TAG, "onRestart");
}
@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    Log.e(TAG, "onStart");
}
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.e(TAG, "onResume");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    Log.e(TAG, "onSaveInstanceState");
}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.e(TAG, "onPause");
}
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Log.e(TAG, "onDestroy");
}
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    Log.e(TAG, "onStop");
}

} step 1: launch MainActivity; the logcat show below:

10-22 13:56:59.704: E/MainActivityonCreate(4649): onCreate
10-22 13:56:59.735: E/MainActivityonStart(4649): onStart
10-22 13:56:59.736: E/MainActivityonResume(4649): onResume

step 2: from MainActivity to SecondActivity; the logcat show below:

10-22 14:03:52.452: E/MainActivityonPause(4884): onPause
10-22 14:03:52.462: E/SECONDACTIVITY(4884): onCreate
10-22 14:03:52.485: E/SECONDACTIVITY(4884): onStart
10-22 14:03:52.485: E/SECONDACTIVITY(4884): onResume
10-2214:03:52.773:E/MainActivityonSaveInstanceState(4884): onSaveInstanceState
10-22 14:03:52.774: E/MainActivityonStop(4884): onStop

step 3: from SecondActivity to MainActivity the logcat show below:

10-22 14:05:14.561: E/SECONDACTIVITY(4884): onPause
10-22 14:05:14.574: E/MainActivityonCreate(4884): onCreate
10-22 14:05:14.592: E/MainActivityonStart(4884): onStart
10-22 14:05:14.593: E/MainActivityonResume(4884): onResume
10-22 14:05:14.877: E/SECONDACTIVITY(4884): onSaveInstanceState
10-22 14:05:14.877: E/SECONDACTIVITY(4884): onStop

step 4: from MainActivity to SecondActivity the logcat show below:

10-22 14:05:51.049: E/MainActivityonPause(4884): onPause
10-22 14:05:51.061: E/SECONDACTIVITY(4884): onRestart
10-22 14:05:51.061: E/SECONDACTIVITY(4884): onStart
10-22 14:05:51.061: E/SECONDACTIVITY(4884): onResume
10-22 14:05:51.330: E/MainActivityonStop(4884): onStop
10-22 14:05:51.330: E/MainActivityonDestroy(4884): onDestroy

my question:why the step 4,the MainActivity call the onDestroy() method ? someone help me?

lgw150
  • 170
  • 1
  • 3
  • 13

3 Answers3

1

I tried your code and my onDestroy() Method does not gets called. In your case onDestroy gets called i guess because system wants to clear some memory After your Step4 your backStack looks like below

Your back stack after method 4 is like this

SecondActivity  Step 4
MainActivity    Step 3
SecondActivity  Step 2
MainActivity    Step 1

According to developer website

onDestroy methods called

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
  • thank you for the reply, i test the code on two different devices,but when i go to the step 4,the onDestroy() alway be called ,so maybe its no the system wants to clear some memory and the call the onDestory() method;by the way ,i set these two activities as singleTask mode,so the back stack must looks like below:SecondActivity and MainActivity only(there only one instance activity on the back stack)? – lgw150 Oct 22 '13 at 07:37
  • @lgw150 did you set FLAG_ACTIVITY_SINGLE_TOP flag for intent using intent.setFlags(FLAG_ACTIVITY_SINGLE_TOP) before calling startActivity() ?
    from [developer.android.com](http://developer.android.com/reference/android/content/Intent.html)
    __If set, the activity will not be launched if it is already running at the top of the history stack.__
    – fadedreamz Oct 22 '13 at 08:53
  • Hmmmm try one thing, in your mobile device close all applications from task manager and run this code again and see if onDestroy() gets called or not. – Jitender Dev Oct 22 '13 at 08:58
  • @fadedreamz:no ,i just set the launch mode in the manifest.xml like this:,and as the google document said,the singTask mode activity is always the root activity on the stack,so i don't add the FLAG as you said. – lgw150 Oct 22 '13 at 09:22
  • @lgw150 as far my experience goes singleTask is not appropriate for normal application, because they *kind-of* hold their states if they are re-invoked from other Activity [Have a look here](http://stackoverflow.com/questions/11668144/about-android-launchmode-singletask) it should give you a good explanation, although I am not sure ... – fadedreamz Oct 22 '13 at 09:35
1

As the system calls the stop method after that the destroy method is called for the activity and it's upto the device when to call the destroy method.

The stop method doesnot delete the memory of the activity on the device but the activity is not working anymore but as soon the destroy method is called all the memory allocation of the activity is destroyed.

jyomin
  • 1,957
  • 2
  • 11
  • 27
0

Its totally dependent on device & # of running application. If you have a high end device (which has >1GB RAM; you probably will not see getting onDestroyed() called. That is because Android can keep more application running within more memory.

Again you can always force onDestroy() getting called by killing task from TaskManager. Alternatively you can set the developer option "Do not Keep activities" on ICS+ devices. This will always destroy your app whenever you leave it.

The main difference between onStop() and onDestroy() is that onStop() only suspends your Activity (UI & UI thread) but it does not touch your running threads (like network thread, Services, receivers e,g ). So, your app can still work (play music or download something) on background even if onStop() is called.

onDestroy() is called by android system to claim memory space (for new activity invoke or itself) and it will destroy the Activity which sits on the bottom of ActivityStack. You can think onDestroy() as a graceful shutdown request from system. You should stop everything if onDestroyed() called. After this call your every thread will be terminated.

Hope this helps, regards.

fadedreamz
  • 1,156
  • 1
  • 10
  • 19