今天通过实例方法系统的看了一下activity的声明周期。就是把整个过程的状态log出来。或者通过对话框显示出来。
源代码贴出来:
注:另一个activity的代码没有贴,大家随便写。注意activity的注册
package com.yelbosh.test;
import com.yelbosh.test.classes.MyButton;
import Android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.support.v4.app.NavUtils;
import android.webkit.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private AlertDialog.Builder builder;
private int count=0;
private final String TAG = "log";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button)findViewById(R.id.button1);
builder = new AlertDialog.Builder(this);
builder.setMessage("你确定这么做吗亲?").
/**
* 肯定的按钮函数
*/
setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
/**
* 否定的按钮函数
*/
setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.setMessage("OnCreate" + (count++));
builder.create().show();
Log.i(TAG,"oncreate");
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
/**
* 通过dialog的builder来构造窗口
*/
Intent intent = new Intent();
intent.setClass(MainActivity.this, SubActivity1.class);
startActivity(intent);
}
});
}
/**
* 当activity变得对用户可见的时候被调用
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
builder.setMessage("onStart" + (count++));
builder.create().show();
Log.i(TAG,"onstart");
}
/**
* 当activity变得对用户不可见的时候被调用
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
builder.setMessage("onStop" + (count++));
builder.create().show();
Log.i(TAG,"onstop");
}
/**
* 当界面准备和用户交互的时候被调用
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
builder.setMessage("OnResume" + (count++));
builder.create().show();
Log.i(TAG,"onresume");
}
/**
* 当系统准备创建另一个屏幕的时候被调用
*/
@Override
protected void onPause(){
//TODO Auto-generated method stub
super.onPause();
builder.setMessage("onPause" + (count++));
builder.create().show();
Log.i(TAG,"onpause");
}
/**
* 当activity被销毁的时候被调用
*/