本文将直接解释我写的一个示例。示例目的在于展示 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. 201521123086《java程序设计》第7周

    本章学习总结 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 以下是ArrayList的contains源代码: public boolean con ...

  2. 201521123052《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 类型转换与强制类型转换(cast) 多态:同一操作作用于不同的对象,可以有不同的解释,产生不 ...

  3. 201521123093 java 第九周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己 ...

  4. 《JAVA程序设计》第10周学习总结

    1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 1.finally 题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中fin ...

  5. 201521123114 《Java程序设计》第12周学习总结

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

  6. 201521123051《Java程序设计》第十周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...

  7. returned a response status of 403 OR 409

    当我们使用jersy把图片上传到我们的图片服务器中[tomcat],我们可能会有以下的错误: returned a response status of 403 OR 409 403和409我都遇到过 ...

  8. PHP 动态调整内存限制

    最近公司的一个PHP项目在操作大文件的时候总是抛出这个异常 Fixing PHP Fatal Error: Allowed Memory Size Exhausted 经过一番调试后发现是达到了PHP ...

  9. Azure SQL Database (24) 使用新管理界面,创建跨数据中心标准地域复制(Standard Geo-Replication)

    <Windows Azure Platform 系列文章目录> 文本是对:SQL Azure (17) SQL Azure V12 - 跨数据中心标准地域复制(Standard Geo-R ...

  10. 【个人笔记】《知了堂》MySQL三种关系:一对一,一对多,多对多。

    一对一:比如一个学生对应一个身份证号.学生档案: 一对多:一个班可以有很多学生,但是一个学生只能在一个班: 多对多:一个班可以有很多学生,学生也可以有很多课程: 一对多关系处理: 我们以学生和班级之间 ...