Android--Activity的生命周期(一)

学了很长时间Android,先回过头来整理一下。

package com.lifecycle.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class FirstActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d(AppConstant.LOG, "FirstActivity-->onCreate()");
        Button nextBtn = (Button) findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
				FirstActivity.this.startActivity(intent);
			}
		});
    }

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(AppConstant.LOG, "FirstActivity-->onStart()");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(AppConstant.LOG, "FirstActivity-->onRestart()");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(AppConstant.LOG, "FirstActivity-->onResume()");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(AppConstant.LOG, "FirstActivity-->onPause()");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(AppConstant.LOG, "FirstActivity-->onStop()");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(AppConstant.LOG, "FirstActivity-->onDestroy()");
	}
}

package com.lifecycle.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Log.d(AppConstant.LOG, "SecondActivity-->onCreate()");
		Button nextBtn = (Button) findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
				SecondActivity.this.startActivity(intent);
			}
		});
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(AppConstant.LOG, "SecondActivity-->onStart()");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(AppConstant.LOG, "SecondActivity-->onRestart()");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(AppConstant.LOG, "SecondActivity-->onResume()");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(AppConstant.LOG, "SecondActivity-->onPause()");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(AppConstant.LOG, "SecondActivity-->onStop()");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(AppConstant.LOG, "SecondActivity-->onDestroy()");
	}
}

package com.lifecycle.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class ThirdActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Log.d(AppConstant.LOG, "ThirdActivity-->onCreate()");
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(AppConstant.LOG, "ThirdActivity-->onStart()");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(AppConstant.LOG, "ThirdActivity-->onRestart()");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(AppConstant.LOG, "ThirdActivity-->onResume()");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(AppConstant.LOG, "ThirdActivity-->onPause()");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(AppConstant.LOG, "ThirdActivity-->onStop()");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(AppConstant.LOG, "ThirdActivity-->onDestroy()");
	}
}

Android--Activity的生命周期(一)_第1张图片

可以看到,有3个Activity,在默认情况下,这3个Activity属于同一个Task,进栈和出栈所执行的生命周期函数如上图。


你可能感兴趣的:(Android--Activity的生命周期(一))