1.activity1
public class MainActivity extends Activity {
/**
* Activity第1次创建时调用
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("FirstActivity->onCreate");
}
public void enter2(View view){
Intent intent=new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
/**
* 1.在代码中明确调用了finish方法
* 2.Android系统资源不够用时,会优先销毁第1个不可见的Activity
*/
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("FirstActivity->onDestroy");
}
/**
* 当启动另外一个Activity时,就会调用第1个Activity的onPause方法
*/
@Override
protected void onPause() {
super.onPause();
System.out.println("FirstActivity->onPause");
}
@Override
protected void onRestart() {
super.onRestart();
System.out.println("FirstActivity->onRestart");
}
/**
* Activity获得用户焦点时,即与用户交互时
*/
@Override
protected void onResume() {
super.onResume();
System.out.println("FirstActivity->onResume");
}
/**
* Activity能被看到时
*/
@Override
protected void onStart() {
super.onStart();
System.out.println("FirstActivity->onStart");
}
/**
* 完全被新的Activity遮挡住时调用
*/
@Override
protected void onStop() {
super.onStop();
System.out.println("FirstActivity->onStop");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
2.secondActivity
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
System.out.println("SecondActivity->onCreate");
}
public void enter1(View view){
Intent intent=new Intent();
intent.setClass(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("SecondActivity->onDestroy");
}
@Override
protected void onPause() {
super.onPause();
System.out.println("SecondActivity->onPause");
}
@Override
protected void onRestart() {
super.onRestart();
System.out.println("SecondActivity->onRestart");
}
@Override
protected void onResume() {
super.onResume();
System.out.println("SecondActivity->onResume");
}
@Override
protected void onStart() {
super.onStart();
System.out.println("SecondActivity->onStart");
}
@Override
protected void onStop() {
super.onStop();
System.out.println("SecondActivity->onStop");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
3.activity_main.xml
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
android:onClick="enter2"
android:text="进入Activity2" />
4.activity_second.xml
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="159dp"
android:onClick="enter1"
android:text="进入Activity1" />