本文将直接解释我写的一个示例。示例目的在于展示 Android 活动在 Xamarin 中的用法。如果有朋友对基础知识不太了解建议先学 Android 。

  新建一个 Xamarin.Android 项目 ActivityStates。在项目中新建文件夹 Activities 用来存放活动。项目结构如下

  (GitHub : https://github.com/ZhangGaoxing/xamarin-android-demo/tree/master/ActivityStates

  在 Activities 文件夹中新建两个活动 DialogActivity 和 NormalActivity(右击 Activities —— 添加 —— 新建项 —— 活动)

  在 Resources 下的 layout 文件夹中新建两个与之相对应的布局 DialogLayout.axml 和 NormalLayout.axml(右击 layout —— 添加 —— 新建项 —— Android 布局)

  完成后项目结构如下图所示

  说完活动、布局的新建,接下来开始编辑代码,新建的活动和布局的代码很简单

  NormalActivity.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget; namespace ActivityStates.Activities
{
[Activity(Label = "NormalActivity")]
public class NormalActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.NormalLayout);
}
}
}

  对应的 NormalLayout.axml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="This is a Normal Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
</LinearLayout>

  下面要说说 DialogActivity.cs 。由于我们需要把当前的活动主题改为 Dialog 形式,按照一般的 Android 项目,我们应该编辑 AndroidManifest.xml 文件,但在 Xamarin 项目中采用 C# 中的特性(Attribute)来进行配置。详细说明可以按 F12 查看代码。

  所以在配置活动主题时我们需要把活动特性修改为以下代码:[Activity(Label = "DialogActivity", Theme ="@android:style/Theme.Dialog")]

  DialogActivity.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget; namespace ActivityStates.Activities
{
[Activity(Label = "DialogActivity", Theme ="@android:style/Theme.Dialog")]
public class DialogActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.DialogLayout);
}
}
}

  DialogLayout.axml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="This is a Dialog Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
</LinearLayout>

  

  下面来说明一下主活动和主布局。在主布局中我们需要两个 Button 用来跳转到上面两个新建的活动,和一个 TextView 用来输出 Log。

  Main.axml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Dialog Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnDialog" />
<Button
android:text="Normal Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnNormal" />
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtLog" />
</LinearLayout>

  而在主活动中我们需要重写活动生命周期的7个方法,在 OnDestroy() 方法中,由于活动已经销毁,向 TextView 中输出完全没有意义,所以采用 Toast 通知的方法来输出。

  MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content; namespace ActivityStates
{
[Activity(Label = "ActivityStates", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
TextView txtLog; // 用于显示信息 protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
// 获取控件
Button btnDialog = FindViewById<Button>(Resource.Id.btnDialog);
Button btnNormal = FindViewById<Button>(Resource.Id.btnNormal);
txtLog = FindViewById<TextView>(Resource.Id.txtLog);
// 输出信息
txtLog.Text += "OnCreate()\n"; btnDialog.Click += (sender, e) =>
{
Intent dialog = new Intent(this, typeof(Activities.DialogActivity));
StartActivity(dialog);
}; btnNormal.Click += (sender, e) =>
{
Intent normal = new Intent(this, typeof(Activities.NormalActivity));
StartActivity(normal);
};
} protected override void OnStart()
{
base.OnStart();
txtLog.Text += "OnCreate()\n";
} protected override void OnResume()
{
base.OnResume();
txtLog.Text += "OnResume()\n";
} protected override void OnPause()
{
base.OnPause();
txtLog.Text += "OnPause()\n";
} protected override void OnStop()
{
base.OnStop();
txtLog.Text += "OnStop()\n";
} protected override void OnRestart()
{
base.OnRestart();
txtLog.Text += "OnRestart()\n";
} protected override void OnDestroy()
{
base.OnDestroy();
Toast.MakeText(this, "OnDestroy()", ToastLength.Short).Show();
}
}
}

  运行图

张高兴的 Xamarin.Android 学习笔记:(三)活动生命周期的更多相关文章

  1. 张高兴的 Xamarin.Android 学习笔记:(一)环境配置

    最近在自学 Xamarin 和 Android ,同时发现国内在做 Xamarin 的不多.我在自学中间遇到了很多问题,而且百度到的很多教程也有些过时,现在打算写点东西稍微总结下,顺便帮后人指指路了. ...

  2. 张高兴的 Xamarin.Android 学习笔记:(二)“Hello World”

    完成环境配置后开始第一个简单项目.打开 Visual Studio 新建一个 Xamarin.Android 项目 "HelloAndroid".(GitHub:https://g ...

  3. android学习笔记28——Activity生命周期

    Activity生命周期 Activity的活动状态由android已Activity栈的形式管理,当前活动的Activity位于栈顶.随着不同应用的运行,每个Activity都有可能从活动状态转入非 ...

  4. Android学习笔记:Activity生命周期详解

    进行android的开发,必须深入了解Activity的生命周期.而对这个讲述最权威.最好的莫过于google的开发文档了. 本文的讲述主要是对 http://developer.android.co ...

  5. android学习笔记(5)Activity生命周期学习

    每一个activity都有它的生命周期,开启它,关闭它,跳转到其他activity等等,都会自己主动调用下面某种方法.对这些个方法覆写后观察学习. protected void onCreate(Bu ...

  6. 张高兴的 Xamarin.Android 学习笔记:(四)常用控件

    示例地址 GitHub : https://github.com/ZhangGaoxing/xamarin-android-demo/tree/master/ControlsDemo

  7. Android学习笔记----Activity的生命周期图示

    转载,一目了然.

  8. MAVEN学习笔记之Maven生命周期和插件简介(3)

    MAVEN学习笔记之Maven生命周期和插件简介(3) clean compile site三套生命周期相互独立. clean pre-clean 执行清理前的工作 clean 清理上一次构建生成的所 ...

  9. Maven学习笔记(六):生命周期与插件

    何为生命周期:      Maven的生命周期就是为了对全部的构建过程进行抽象和统一.Maven从大量项目和构建工具中学习和反思,然后总结了一套高度完好的.易扩展的生命周期.这个生命周期包括了项目的清 ...

随机推荐

  1. 团队作业8——第二次项目冲刺(Beta阶段)5.18

    1.当天站立式会议照片 会议内容: 本次会议为第一次会议 本次会议在陆大楼2楼召开,本次会议内容: ①:部署第二次敏捷冲刺的计划 ②:做第一天任务的详细分工 ③:规定完成时间是在第二天之前 ④:遇到困 ...

  2. Java课程设计----仿Windows标准型计算器

    JAVA课程设计 仿Windows标准型计算器(By Yanboooooooo) 一.团队介绍: 连燕波[组长]:网络1513学生. 张文博[组员]:网络1513学生. 二.项目git地址 码云项目地 ...

  3. 201521123070 《JAVA程序设计》第12周学习总结

    1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  4. python基础之元组,集合

    一.元组 为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(,[,],,)) #t=tuple((,[,],,))) print(type(t)) 元组可以作为字典的key d={(,, ...

  5. Oracle总结第三篇【PLSQL】

    PLSQL介绍 PLSQL是Oracle对SQL99的一种扩展,基本每一种数据库都会对SQL进行扩展,Oracle对SQL的扩展就叫做PLSQL- SQL99是什么 (1)是操作所有关系型数据库的规则 ...

  6. temp-存储过程 以前的

    ---------------------------------------------------------------------------------------------------- ...

  7. YYHS-NOIP2017Training0921-逆光

    题目描述 有一束光/那瞬间/是什么痛得刺眼/你的视线是谅解/为什么舍不得熄灭/我逆着光却看见/那是泪光/那力量/我不想再去抵挡/面对希望/逆着光/感觉爱存在的地方/一直就在我身旁 Descriptio ...

  8. angular 学习笔记

    每天进步一点点,学习笔记 笔记来自  angular权威指南 如果想要屏蔽浏览器对表单的默认验证行为,可以在表单元素上添加 novalidate 标记. 而按钮标签则完全忽略 hr e f 属性,并不 ...

  9. S3C2440 时钟设置分析(FCLK, HCLK, PCLK)

    时钟对于一个系统的重要性不言而喻,时钟决定了系统发送数据的快慢,高性能的芯片往往能支持更快速度的时钟,从而提供更好的体验. S3C2440的输入时钟频率是12MHZ,对于这款芯片,显然速度是不够的,所 ...

  10. uva1267 Network

    https://vjudge.net/problem/UVA-1267 题意: 有一棵树,上面有一个放着水源的点s,给出一个数k,这个水源可以覆盖路径长度到s不超过k的叶子节点.现在需要把所有的叶子节 ...