FirstActivity.java
package mars.activity04;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
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{
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(FirstActivity.this,SecondActivity.class);
FirstActivity.this.startActivity(intent);
}
}
}
SecondActivity.java
package mars.activity04;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Gallery;
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{
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(SecondActivity.this,FirstActivity.class);
SecondActivity.this.startActivity(intent);
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/secondButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, FirstActivity!</string>
<string name="app_name">activity04</string>
<string name="myButton">myButton</string>
<string name="firstActivityLabel">FirstActivity</string>
<string name="secondActivityLabel">SecondActivity</string>
<string name="exit">退出</string>
<string name="about">关于</string>
</resources>