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


    进行android的开发,必须深入了解Activity的生命周期。而对这个讲述最权威、最好的莫过于google的开发文档了。

    本文的讲述主要是对 http://developer.android.com/training/basics/activity-lifecycle/index.html 的翻译,加上了个人的一些细节和补充。

    一、概述

    Managing the Activity Lifecycle

    管理Activity的生命周期

    As a user navigates through, out of, and back to your app, the Activity instances in your app transition between different states in their lifecycle.

    当用户进入、离开和返回一个app程序时,程序中的Activity实例会在它们的生命周期中的各个状态之间切换。

    For instance, when your activity starts for the first time, it comes to the foreground of the system and receives user focus.

    比如,当第一次启动Activity时,它会处于系统的前台,并且能接受用户的操作聚焦。

     During this process, the Android system calls a series of lifecycle methods on the activity in which you set up the user interface and other components.

    在这个过程中,android系统会调用Activity的一系列生命周期会调方法,从而创建用户界面和其它的组件。

    If the user performs an action that starts another activity or switches to another app, the system calls another set of lifecycle methods on your activity as it moves into the background (where the activity is no longer visible, but the instance and its state remains intact).

    当用户执行动作启动另外一个Activity或切换到另外一个app时,该Activity,会移到系统后台(这时Activity将不再可视,但它的实例和状态将原封不动的保存着),这时系统将会调用该Activity 的另外一系列生命周期会调方法,

    Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity.

    在Activity的生命周期会调方法内,你可以定义当用户离开和重新进入Activity时,你想要做的事情。

    For example, if you're building a streaming video player, you might pause the video and terminate the network connection when the user switches to another app.

    比如,如果你在编写一个实时流视频播放程序,在视频正在播放时,用户切换到另外一个app(如用户按home键、电话来了等),这时程序要能够暂停视频播放,并且断开网络链接。

    When the user returns, you can reconnect to the network and allow the user to resume the video from the same spot.

    当用户重新返回到该视频播放程序,程序应该能够恢复网络链接,并且从前面停止的点开始继续恢复视频播放。

    This class explains important lifecycle callback methods that each Activity instance receives and how you can use them so your activity does what the user expects and does not consume system resources when your activity doesn't need them.

    本课程将会将会介绍Activity的每个重要的生命周期回调方法,并且告诉你如何处理,从而处理的结果与用户期望保持一致。而且当Activity不再需要时,能不让其继续耗费系统资源。

    二、Starting an Activity

    启动Activity

    Unlike other programming paradigms in which apps are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle.

    很多其它的编程语言程序从main方法开始运行,而Android程序不同,Android系统通过回调Activity特定的生命周期方法来实例化一个Activity,并且根据其位于不同的生命周期作出不同的反应。

     There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity.

    当启动一个Activity时,会触发一系列的回调方法;同样,当关闭一个Activity时,也会触发一系列的回调方法。

    This lesson provides an overview of the most important lifecycle methods and shows you how to handle the first lifecycle callback that creates a new instance of your activity.

    本小结将会大致的介绍下Activity的各个重要的生命周期方法,并且通过处理第一个生命周期方法来实例化一个Activity。

  • 相关阅读:
    leetcode hot 100- 84. 柱状图中最大的矩形
    leetcode hot 100- 221. 最大正方形
    leetcode hot 100-34. 在排序数组中查找元素的第一个和最后一个位置
    leetcode hot 100-剑指 Offer 37. 序列化二叉树
    leetcode hot 100-2. 两数相加
    leetcode hot 100-33. 搜索旋转排序数组
    leetcode hot 100- 98. 验证二叉搜索树
    leetcode hot 100-152. 乘积最大子数组
    leetcode hot 100-19. 删除链表的倒数第N个节点
    v-modal的使用。
  • 原文地址:https://www.cnblogs.com/51kata/p/4130075.html
Copyright © 2020-2023  润新知