【Android API重新回炉学习1】Activity



http://developer.android.com/guide/components/activities.html

NotePad:

1.在AndroidManifest.xml中定义一个Activity

 

<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
To declare your activity, open your manifest file and add an  <activity>  element as a child of the  <application>  element。There are several other attributes that you can include in this element, to define properties such as the label for the activity, an icon for the activity, or a theme to style the activity's UI. The android:name attribute is the only required attribute—it specifies the class name of the activity. Once you publish your application, you should not change this name, because if you do, you might break some functionality, such as application shortcuts

另外是

        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />

这两个标签的含义, The  <action>  element specifies that this is the "main" entry point to the application. The  <category>  element specifies that this activity should be listed in the system's application launcher


2. finish()和finishActivity()方法

   finish()结束当前Acitivity的生命周期

   finishActivity(int requestCode)结束之前使用startAcitivity(Intent intent,int requestCode)方法创建的Activity


3.Activity的生命周期

老生常谈的问题了,一张图解决所有的生命周期,onCreate 对应 onDestroy onStart 对应 onStop(完全不可见),onResume(Activit到前台并开始接受事件)对应onPause(进入后台并且不能接受事件,但不一定是不可见状态) 

有趣的一点是两个Activity交互的生命周期,即一个Activity启动另一个Acitivity,这两个Acitivity纠缠在一起的生命周期是什么样的。

给出的例子是假如Activity A启动Acitivity B 那么 生命周期是下面的样子

1.Activity A's onPause() method executes.

2.Activity B's onCreate()onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)

3.Then, if Activity A is no longer visible on screen, its onStop() method executes.

Guide中给出了一个适用场景

 For example, if you must write to a database when the first activity stops so that the following activity can read it, then you should write to the database during onPause() instead of during


4.Acitivity状态的保留

  当Activity的生命周期走到onPause()之后,这个Activity就属于是后台的Activity了,对于系统来说,后台的Activity优先级很低,资源不足的时候随时可以杀死它。但是如果这个Acitivity要重新回到前台的时候,对于用户来说,用户并不知道这个Activity是否是被系统杀死过,所以,用户希望看到的就是Activity展现出来的状态和之前隐藏到后台之前的状态是一致的。

  而且,Activity在一些特定的场景中是必须被杀死重启的,Guide中给出的例子有

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android recreates the running activity (the system calls onDestroy(), then immediately calls onCreate()).


  保存Activity状态使用的方法是 onSaveInstanceState(Bundle outState)这个方法,可以将当前需要保留的一些信息以键值对的形式保存到这个Bundle中去,在Acitivity重新启动的时候,可以在onCreate(Bundle savedInstanceState)方法或者是onRestoreInstanceState(Bundle savedInstanceState)方法中取出这个Bundle里面的值,从而重新构造UI,造成这个Acivity丝毫未变的假象。

  还是一张图揭示所有的过程

  


需要注意的事项

Note: There's no guarantee that onSaveInstanceState() will be called before your activity is destroyed, because there are cases in which it won't be necessary to save the state (such as when the user leaves your activity using the Back button, because the user is explicitly closing the activity). If the system callsonSaveInstanceState(), it does so before onStop() and possibly before onPause().

我们经常听说这个方法却不经常使用的原因是,即使是不继承或者重写这个方法,一些Activity的状态依旧能够保存。原因是

The default implementation calls the corresponding onSaveInstanceState() method for every View in the layout, which allows each view to provide information about itself that should be saved. Almost every widget in the Android framework implements this method as appropriate, such that any visible changes to the UI are automatically saved and restored when your activity is recreated. For example, the EditText widget saves any text entered by the user and the CheckBox widget saves whether it's checked or not. The only work required by you is to provide a unique ID (with the android:id attribute) for each widget you want to save its state. If a widget does not have an ID, then the system cannot save its state.

特殊情况下,如果不想保存当前控件的状态的话,可以使用下面的方法来关闭保存开关

You can also explicitly stop a view in your layout from saving its state by setting the android:saveEnabledattribute to "false" or by calling thesetSaveEnabled() method. Usually, you should not disable this, but you might if you want to restore the state of the activity UI differently.

由于这个方法并不是每次退出Activity的时候都会被调用,所以保存数据的话,不要使用这个方法

Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.

Q:

1.Activity默认究竟有几种style 例如Dialog的风格,都是通过哪些参数定制实现的

2.Activity的四种启动模式的描述以及其间的区别是什么


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教资源的高效利用。同时,教质量评估中心和资源应用平台的建设,旨在提升教评估的科性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教和管理流程,为生、教师和家长提供了更加便捷、个性化的教育体验。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值