Activity 的介绍免了(想看点这里),下面直接上代码:
Activity 的生命周期
在 MainActivity 以及 SecondActivity 中的各个生命周期方法中,用 log 的方式输出各自的方法名,根据输出的顺序,来观察生命周期方法调用的顺序:
MainActivity
- /**
- * @author sumile
- * @WEB https://sumile.cn
- * @2015 年 7 月 6 日 上午 10:58:06
- * @TODO
- */
- public class MainActivity extends Activity {
- private final String TAG = "sumile";
- private TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Log.i(TAG, "MainActivity OnCreate");
- tv = (TextView) findViewById(R.id.tv);
- tv.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 本方法于 2015 年 7 月 6 日 上午 11:04:48 由 sumile 建立
- Intent intent = new Intent(MainActivity.this, SecondActivity.class);
- startActivity(intent);
- }
- });
- }
- @Override
- protected void onStart() {
- // 本方法于 2015 年 7 月 6 日 上午 10:59:43 由 sumile 建立
- super.onStart();
- Log.i(TAG, "MainActivity onStart");
- }
- @Override
- protected void onResume() {
- // 本方法于 2015 年 7 月 6 日 上午 10:59:49 由 sumile 建立
- super.onResume();
- Log.i(TAG, "MainActivity onResume");
- }
- @Override
- protected void onPause() {
- // 本方法于 2015 年 7 月 6 日 上午 10:59:55 由 sumile 建立
- super.onPause();
- Log.i(TAG, "MainActivity onPause");
- }
- @Override
- protected void onStop() {
- // 本方法于 2015 年 7 月 6 日 上午 11:00:00 由 sumile 建立
- super.onStop();
- Log.i(TAG, "MainActivity onStop");
- }
- @Override
- protected void onDestroy() {
- // 本方法于 2015 年 7 月 6 日 上午 11:00:03 由 sumile 建立
- super.onDestroy();
- Log.i(TAG, "MainActivity onDestroy");
- }
- @Override
- protected void onRestart() {
- // 本方法于 2015 年 7 月 6 日 上午 11:00:07 由 sumile 建立
- super.onRestart();
- Log.i(TAG, "MainActivity onRestart");
- }
- }
SecondActivity
- /**
- * @sumile
- * @WEB https://sumile.cn
- * @2015 年 7 月 6 日 上午 11:01:44
- * @TODO
- */
- public class SecondActivity extends Activity {
- private final String TAG = "sumile";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- Log.i(TAG, "SecondActivity OnCreate");
- }
- @Override
- protected void onStart() {
- // 本方法于 2015 年 7 月 6 日 上午 10:59:43 由 sumile 建立
- super.onStart();
- Log.i(TAG, "SecondActivity onStart");
- }
- @Override
- protected void onResume() {
- // 本方法于 2015 年 7 月 6 日 上午 10:59:49 由 sumile 建立
- super.onResume();
- Log.i(TAG, "SecondActivity onResume");
- }
- @Override
- protected void onPause() {
- // 本方法于 2015 年 7 月 6 日 上午 10:59:55 由 sumile 建立
- super.onPause();
- Log.i(TAG, "SecondActivity onPause");
- }
- @Override
- protected void onStop() {
- // 本方法于 2015 年 7 月 6 日 上午 11:00:00 由 sumile 建立
- super.onStop();
- Log.i(TAG, "SecondActivity onStop");
- }
- @Override
- protected void onDestroy() {
- // 本方法于 2015 年 7 月 6 日 上午 11:00:03 由 sumile 建立
- super.onDestroy();
- Log.i(TAG, "SecondActivity onDestroy");
- }
- @Override
- protected void onRestart() {
- // 本方法于 2015 年 7 月 6 日 上午 11:00:07 由 sumile 建立
- super.onRestart();
- Log.i(TAG, "SecondActivity onRestart");
- }
- }
运行成功后,在不同操作下,生命周期方法的调用顺序如下:
转载请注明:热爱改变生活.cn » Activity 初探
本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » Activity 初探