Android 开发文档 程序基础——Shutting down components

来源:互联网 发布:广西财经学院网络 编辑:程序博客网 时间:2024/05/16 19:36

content provider只会在回应ContentReslover发出的请求时激活,而broadcast receiver只在回应广播消息时激活,所以没有必要关闭这些组件。

另一方面,activity用来生成用户界面,长时间的与用户对话,甚至空闲时也保持活动,只要对话还在继续。相似的,service也会保持长时间的运行。所以android提供了方法来关闭activity和service。

activity可以通过调用finish()来关闭。一个activity可以通过调用finishActivty()关闭另一个activty(它通过startActivityForResult()运行起来的)。

service可以通过调用自身的stopSelf()来停止,或者Context.stopService().

转自我的android博客

原文

Shutting down components

A content provider is active only while it’s responding to a request from a ContentResolver. And a broadcast receiver is active only while it’s responding to a broadcast message. So there’s no need to explicitly shut down these components.

Activities, on the other hand, provide the user interface. They’re in a long-running conversation with the user and may remain active, even when idle, as long as the conversation continues. Similarly, services may also remain running for a long time. So Android has methods to shut down activities and services in an orderly way:

  • An activity can be shut down by calling its finish() method. One activity can shut down another activity (one it started with startActivityForResult()) by calling finishActivity().
  • A service can be stopped by calling its stopSelf() method, or by calling Context.stopService().

Components might also be shut down by the system when they are no longer being used or when Android must reclaim memory for more active components. A later section, Component Lifecycles, discusses this possibility and its ramifications in more detail.

原创粉丝点击