天天看点

Xamarin.Android Activity入门介绍

Activity是一个应用程序的组件,他在屏幕上提供了一个区域,允许用户在上面做一些交互性的操作, 比如打电话,照相,发送邮件,或者显示一个地图!Activity可以理解成一个绘制用户界面的窗口, 而这个窗口可以填满整个屏幕,也可能比屏幕小或者浮动在其他窗口的上方!

Activity生命周期介绍

Xamarin.Android Activity入门介绍
建议自己写Demo查看各个生命周期调用时间。

OnCreate

当Activity创建时会调用此方法,是我们必须要重写的方法,该方法传入一个Bundle对象作为参数。如果bundle不为null,说明当前Activity是重新启动。可以通过bundle读取我们之前保存的数据,做相应的数据恢复工作。在这个方法中要执行的操作是:

Creating views

Initializing variables

Binding static data to lists

OnStart

OnCreate执行结束会掉用OnStart方法,可以根据具体应用情况重写该方法。

OnResume

当一个Activity初始化完成准备显示给用户时会掉用OnResume方法。此时Activity进入活动状态。在这个方法中我们要做的是:

Ramping up frame rates (a common task in game building)

Starting animations

Listening for GPS updates

Display any relevant alerts or dialogs

Wire up external event handlers

OnPause

当Activity进入后台或者Activity被遮挡时(Dialog风格的Activity)掉用此方法。OnPause方法之行过后Activity可以有两种不同的状态,进入OnResume返回前台或者进入OnStop方法,重写这个方法我们要做的是:

Commit unsaved changes to persistent data

Destroy or clean up other objects consuming resources

Ramp down frame rates and pausing animations

Unregister external event handlers or notification handlers (i.e. those that are tied to a service). This must be done to prevent Activity memory leaks.

Likewise, if the Activity has displayed any dialogs or alerts, they must be cleaned up with the .Dismiss() method.

最后一条,如果不关闭已经显示的Dialog会抛异常。

OnStop

Activity不再显示时调用此方法,此时Activity仍存留在内存中。

OnDestroy

Activity销毁调用次方法,此时Activity已经完全从内存中移除,我们应该重写此方法,释放占用的资源。

OnRestart

是OnStop状态的Activity重新显示给用户会进入OnRestart方法。如按下Home键应用进入后台,重新打开应用,Activity重新启动。

Activity使用

Android中的四大组件,只要你定义了就必须在AndroidManifest.xml中对这个组件进行声明。

定义Activity

我们自定义的Activity一定要继承Activity或其子类(如AppCompatActivity),并重写相关方法。OnCreate是必须重写的,在OnCreate中调用SetContentView方法加载相关联的视图。

声明Activity

在AndroidManifest中增加Activity节点。

参考文档

Xamarin.Android中我们可以通过声明特性进行组件的声明。

1      
[Activity (Label = "activity1", MainLauncher = true, 
Icon = "@mipmap/icon")]      

项目编译时会根据我们声明的特性生成新的AndroidManifest.xml文件

可在项目目录下 /项目名/obj/Debug or Release/android下查看

启动Activity

启动方式:

  • 显式启动
1
2      
Intent intent = new Intent (this, typeof(Activity2));
StartActivity (intent);      
1      
StartActivity(typeof(Activity2));      

Intent对象可以在两个Activity间传递数据。

  • 隐式启动

给出Java示例

Xamarin.Android Activity入门介绍

生命周期中状态的保存

Bundle保存状态

通过OnSaveInstanceState、OnRestoreInstanceState两个方法管理保存的状态(不是一定会执行的)

以下代码,改变默认创建的Android项目,是一个简单保存状态的例子,可以通过横竖屏切换掉用OnSaveInstanceState、OnRestoreInstanceState方法。(这里提到横竖屏切换,我们可以为一个Activity分别提供不同情况下的布局文件,创建两个布局文件夹:layout-land横屏,layout-port竖屏 然后把两套布局文件丢这两文件夹里,文件名一样,Android就会自行判断加载相应布局)

Xamarin.Android Activity入门介绍
事实上我们并不需要在OnSaveInstanceState方法中保存每个view的状态,只要指定view的ID,android系统会自动帮我们保存view的状态,如我们在布局文件中了添加一个EditText视图,并指定ID属性,运行项目,修改EditText的值,切换屏幕方向EditText中的值依然保存。
Xamarin.Android Activity入门介绍

如图所示,Activity由Resumed状态变为Destroyed状态可能会调用OnSaveInstanceState方法,Created到Resumed可能会调用OnRestoreInstanceState方法。

使用Bundle保存状态的限制

It is not called in all cases. For example, pressing home or back to exit an Activity will not result in OnSaveInstanceState being called.

The bundle passed into OnSaveInstanceState is not designed for large objects, such as images. In the case of large objects, saving the object from OnRetainNonConfigurationInstance is preferable, as discussed below.

Data saved by using the bundle is serialized, which can lead to delays.

复杂数据的持久化

由于Bundle的限制,只适合保存简单的数据值如:int,string等。对于复杂数据的保存我们可以通过重写OnRetainNonConfigurationInstance返回一个Java.Lang.Object,恢复数据时LastNonConfigurationInstance返回保存的数据。这里列出一个Xamarin官网的例子:

1.先定义我们要返回的类型

1
2
3
4      
class TweetListWrapper : Java.Lang.Object
{
  public string[] Tweets { get; set; }
}      

2.Activity中处理数据

Xamarin.Android Activity入门介绍

可以注意到OnRetainNonConfigurationInstance方法已经是过期的,查看文档可以看到:

Called by the system, as part of destroying an activity due to a configuration change, when it is known that a new instance will immediately be created for the new configuration. You can return any object you like here, including the activity instance itself, which can later be retrieved by calling Activity.LastNonConfigurationInstance in the new activity instance. If you are targeting Build+VERSION_CODES.Honeycomb or later, consider instead using a Fragment with Fragment.RetainInstance.

系统提供常用Activity