活动的生命周期
OnCreate()
在OnCreate 中主要功能是创建视图并将变量绑定到列表上
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.A_ASRS_001);
this._Init();
}
//绑定
private void _Init()
{
this._Init_Controls();
this.Clean(1);
mGrid.SelectionChanging += new GridSelectionChangingEventHandler(this.GridViewClick);
this.btnInPut.Click += new System.EventHandler(this.btnInPutClick);
this.btnHome.Click += new System.EventHandler(this.btnHomeClick);
this.btnChange.Click += new System.EventHandler(this.btnChangeClick);
}
private void _Init_Controls()
{
this.mGrid = FindViewById<SfDataGrid>(Resource.Id.sfDataGrid001);
this.txtBillID = FindViewById<EditText>(Resource.Id.txtBillId001);
this.txtPalletID = FindViewById<EditText>(Resource.Id.txtPalletID001);
this.txtMatName = FindViewById<EditText>(Resource.Id.txtMatName001);
this.txtNum = FindViewById<EditText>(Resource.Id.txtNum001);
this.txtBatch = FindViewById<EditText>(Resource.Id.txtBatch001);
this.btnInPut = FindViewById<Button>(Resource.Id.btnInPut001);
this.btnHome = FindViewById<Button>(Resource.Id.btnHome001);
this.btnChange = FindViewById<Button>(Resource.Id.btnChange001);
this.btnPallet = FindViewById<Button>(Resource.Id.btnPallet001);
}
其中Bundle类型是用来存储上一个实例中保存的数据,用于返回用户上次操作的状态
OnStart()
在创建视图后调用
OnResume()
当活动准备开始与用户交互时会触发该方法,一般只会在需要执行以下的操作时才重写该事件:
l 开始动画
l 开始监听GPS更新
l 显示一些相关的提示和对话框
l 注册广播监听
在触发OnPause后,用户又切回应用,那么就需要OnResume进行恢复。
OnPause()
当活动被切换到后台时将触发该方法,一般我们需要在该事件做如下的事情:
l 保存用户未提交的数据
l 关闭或清除引用的资源
l 注销广播
l 如果存在正在显示的提示或者对话框,则必须利用.Dismiss()进行清除
OnStop()
当该活动长时间没有在显示给用户下就会触发,一般会由以下原因触发:
l 当一个新的活动打开,并覆盖该活动时
l 一个已存在的活动切换到前台时
l 活动被销毁时
OnStop不是每次都会被执行,如果内存低下时,系统将不会执行该事件,而是直接关闭该应用
OnDestroy()
很少会执行
OnRestart
当用户通过Home按钮将该用户切换到后台,并在之后又打开该应用则会触发该事件
生命周期状态
网上找个例子, 主要是app中count丢失值,但是可以通过OnSaveInstanceState()方法进行重写保存
首先新建一个Android项目,然后打开Main.axml,拖拽一个Text(Large),并在下方拖拽一个Button,设置Button的Text为add,然后生成一遍。
打开MainActivity.cs文件,并写入如下代码:
[Activity(Label = "Activity_Liftcycle", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
private TextView tv;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
tv = FindViewById<TextView>(Resource.Id.textView1);
tv.Text = count.ToString();
Button btn = FindViewById<Button>(Resource.Id.button1);
btn.Click += (e, s) =>
{
count++;
tv.Text = count.ToString();
};
}
}
这里不用解释,大家也知道,其实就是一个累加。现在我们F5运行,然后点击几次Add之后如下所示:
然后我们选择模拟器,按下 Ctrl + F12 是不是发现模拟机翻转了,但是你也会发现数字变成了1:
如果你是在使用一个app,翻转之后出现这个样子,你一定会非常奇怪。当然你可能会想到使用静态变量,但是你考虑过一个实际情况没,
保存一个还好,如果很多呢。那会消耗更多的内存,而且也没有意义。所以这里我们就需要重写上面的OnSaveInstanceState方法,以
便保存当前用户的数据,下面将代码修改如下:
[Activity(Label = "Activity_Liftcycle", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
private TextView tv;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
//如果存在则恢复之前的状态
if (bundle != null)
{
count = bundle.GetInt("_count");
}
tv = FindViewById<TextView>(Resource.Id.textView1);
tv.Text = count.ToString();
Button btn = FindViewById<Button>(Resource.Id.button1);
btn.Click += (e, s) =>
{
count++;
tv.Text = count.ToString();
};
}
protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
//保存当前状态
outState.PutInt("_count", count);
}
}