FirstActivity.java
public class FirstActivity extends Activity { /** Called when the activity is first created. */ private Button myButton; @Override public void onCreate(Bundle savedInstanceState) { System.out.println("FirstAcvity ---> onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton = (Button)findViewById(R.id.myButton); myButton.setText("启动第二个Activity"); myButton.setOnClickListener(new ButtonOnClickListener()); } @Override protected void onDestroy() { // TODO Auto-generated method stub System.out.println("FirstAcvity --->onDestory"); super.onDestroy(); } @Override protected void onPause() { // TODO Auto-generated method stub System.out.println("FirstAcvity --->onPause"); super.onPause(); } @Override protected void onRestart() { // TODO Auto-generated method stub System.out.println("FirstAcvity --->onRestart"); super.onRestart(); } @Override protected void onResume() { // TODO Auto-generated method stub System.out.println("FirstAcvity --->onResume"); super.onResume(); } @Override protected void onStart() { // TODO Auto-generated method stub System.out.println("FirstAcvity --->onStart"); super.onStart(); } @Override protected void onStop() { // TODO Auto-generated method stub System.out.println("FirstAcvity --->onStop"); super.onStop(); } class ButtonOnClickListener implements OnClickListener{ @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(FirstActivity.this,SecondActivity.class); FirstActivity.this.startActivity(intent); } } }
SecondActivity.java
public class SecondActivity extends Activity { private Button secondButton; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub System.out.println("SecondActivity--->onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.second); secondButton = (Button)findViewById(R.id.secondButton); Gallery g = (Gallery)findViewById(R.id.secondButton); secondButton.setOnClickListener(new ButtonOnClickListener()); } @Override protected void onDestroy() { // TODO Auto-generated method stub System.out.println("SecondActivity--->onDestory"); super.onDestroy(); } @Override protected void onPause() { // TODO Auto-generated method stub System.out.println("SecondActivity--->onPause"); super.onPause(); } @Override protected void onRestart() { // TODO Auto-generated method stub System.out.println("SecondActivity--->onRestart"); super.onRestart(); } @Override protected void onResume() { // TODO Auto-generated method stub System.out.println("SecondActivity--->onResume"); super.onResume(); } @Override protected void onStart() { // TODO Auto-generated method stub System.out.println("SecondActivity--->onStart"); super.onStart(); } @Override protected void onStop() { // TODO Auto-generated method stub System.out.println("SecondActivity--->onStop"); super.onStop(); } class ButtonOnClickListener implements OnClickListener{ @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(SecondActivity.this,FirstActivity.class); SecondActivity.this.startActivity(intent); } } }
启动第一个Activity,然后启动第二个Activity,然后按返回键,然后再启动第二个Activity...查看输出结果。