觀察Lifecycle

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //在onCreate階段於android monitor logcat  顯示出onCreate字串 
        Log.e("TAG","onCreate");
    }
    @Override
    protected void onStart() {
        super.onStart();
         //在onStart階段於android monitor logcat  顯示出onStart字串 
        Log.e("TAG", "onStart");

    }
    @Override
    protected void onResume() {
        super.onResume();
         //在onResume階段於android monitor logcat  顯示出onResume字串 
        Log.e("TAG", "onResume");

    }
    @Override
    protected void onStop() {
        super.onStop();
         //在onStop階段於android monitor logcat  顯示出onStop字串 
        Log.e("TAG", "onStop");

    }
    @Override
    protected void onPause() {
        super.onPause();
         //在onPause階段於android monitor logcat  顯示出onPause字串 
        Log.e("TAG", "onPause");

    }
    @Override
    protected void onRestart() {
        super.onRestart();
         //在onRestart階段於android monitor logcat  顯示出onRestart字串 
        Log.e("TAG", "onRestart");

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
         //在onDestroy階段於android monitor logcat  顯示出onDestroy字串 
        Log.e("TAG", "onDestroy");

    }
}

開啟Activity

02-25 14:22:49.217 17278-17278/com.example.user.demo_example E/TAG: onCreate
02-25 14:22:49.218 17278-17278/com.example.user.demo_example E/TAG: onStart
02-25 14:22:49.293 17278-17278/com.example.user.demo_example E/TAG: onResume

關閉Activity

02-25 14:24:48.191 17278-17278/com.example.user.demo_example E/TAG: onPause
02-25 14:24:48.917 17278-17278/com.example.user.demo_example E/TAG: onStop
02-25 14:24:48.917 17278-17278/com.example.user.demo_example E/TAG: onDestroy

啟動另一個Activity

02-25 14:26:17.947 17278-17278/com.example.user.demo_example E/TAG: onPause
02-25 14:26:18.809 17278-17278/com.example.user.demo_example E/TAG: onStop

從另一個Activity返回

02-25 14:26:31.356 17278-17278/com.example.user.demo_example E/TAG: onRestart
02-25 14:26:31.357 17278-17278/com.example.user.demo_example E/TAG: onStart
02-25 14:26:31.357 17278-17278/com.example.user.demo_example E/TAG: onResume