activity源码

Activity 简介

An activity is a single, focused thing that the user can do. Almost all
activities interact with the user, so theActivityclass takes care of
creating a window for you in which you can place your UI with
{@link #setContentView}. While activities are often presented to the user
as full-screen windows, they can also be used in other ways: as floating
windows (via a theme with {@link android.R.attr#windowIsFloating} set)
or embedded inside of another activity (using {@link ActivityGroup}).

Activity是单一,专注于使用者的类。几乎所有的activity都是与使用者做交互,所以,Activity类关系UI放到window上的哪个位置setContentView 。大部分时间界面都是全屏,但他其实还可以展示浮窗floating window{@link android.R.attr#windowIsFloating} 或者嵌入另一个活动activity {@link ActivityGroup}

有两种方法几乎所有Activity的子类都会实现
  • onCreate

    {@link #onCreate} is where you initialize your activity. Most
    importantly, here you will usually call {@link #setContentView(int)}
    with a layout resource defining your UI, and using {@link#findViewById}
    to retrieve the widgets in that UI that you need to interact with programmatically.

    onCreate是你初始activity的地方,在这个方法里通常会调用setContentView(int)来设置自己的UI布局,并且会使用findViewById找到布局中的控件,然后写交互代码。

  • onPause
    {@link #onPause} is where you deal with the user leaving your
    activity. Most importantly, any changes made by the user should at this
    point be committed (usually to the
    {@link android.content.ContentProvider} holding the data).

    当用户暂时离开活动Activity的时候。重点是,用户做的任何改变都要在这里提交,一般使用android.content.ContentProvider持有数据

必不可少的
  • To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all
  • activity classes must have a corresponding
  • {@link android.R.styleable#AndroidManifestActivity <activity>}
  • declaration in their package’s AndroidManifest.xml.
    为了使用Context中 startActivity方法,所有Activity必须在AndroidMainfext.xml中写相应的{@link android.R.styleable#AndroidManifestActivity <activity>}
在Activity之上,有哪些操作呢

开发者指导

The Activity class is an important part of an application's overall lifecycle,and the way activities are launched and put together is a fundamental part of the platform’s application model. For a detailed perspective on the structure of an Android application and how activities behave, please read the Application Fundamentals and Tasks and Back Stack

Activity类是应用程序整个生命周期的重要组成部分,活动的启动和组合方式是平台应用程序模型的基本组成部分。有关Android应用程序结构以及活动行为的详细信息,请阅读应用程序基础知识任务以及返回堆栈

You can also find a detailed discussion about how to create activities in the Activities developer guide.

您还可以在“活动开发人员指南”中找到有关如何创建活动的详细讨论。

Fragments

The{@link android.support.v4.app.FragmentActivity}subclass can make use of the{@link android.support.v4.app.Fragment} class to better modularize their code, build more sophisticated user interfaces for larger screens, and help scale their application between small and large screens.

{@link android.support.v4.app.FragmentActivity}子类可以更好地利用{@link android.support.v4.app.Fragment}类模块化他们的代码,为更大的用户界面构建更复杂的用户界面屏幕,并帮助在小屏幕和大屏幕之间扩展应用程序。

For more information about using fragments, read the
https://developer.android.com/guide/components/fragments.html“>Fragments developer guide.

想要了解更多的关于fragments消息,查看Activity Lifecycle 活动生命周期

Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity – the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

系统中的Activitys(活动)作为Activity stack活动堆栈进行管理。当一个新活动开始时,它被放置在堆栈顶部并成为正在运行(活跃)的Activity , 上一个Activity活动始终保持在堆栈中低于它,新活动退出之前上一个活动不会再次到达前台(回到前台就是处于活跃状态)

活动的四个状态
  • activie or running 活跃状态
    If an activity is in the foreground of the screen (at the top of the stack)
    处于堆栈顶端,在屏幕最上方。
  • paused 暂停状态
    If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme
    low memory situations.

    如果Activity失去焦点但仍然可见(即,一个新的非全尺寸或是透明的Activity,则Activity处于暂停状态。暂停状态是真的存在的(它维护所有成员信息并保持窗口工作),但可以被极端的系统(低内存的时候)杀死。

  • stopped 停止状态
    If an activity is completely obscured by another activity,it is stopped. It still retains all state and member information,however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.

    如果Activity被另一个Activity完全遮挡,则会停止活动。它仍保留所有状态和成员信息,但是,它不再对用户可见,因此其窗口被隐藏,当其他地方需要内存时,它通常会被系统杀死

  • 死亡状态

    If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state

下图显示了Activity和Fragment生命周期。

这里写图片描述

三个关键循环

There are three key loops you may be interested in monitoring within your activity
Activity周期中有三个关键循环,你可能有兴趣利用他来做监控。

  • 整个生命周期

The entire lifetime of an activity happens between the first call to {@link android.app.Activity#onCreate} through to a single final call to{@link android.app.Activity#onDestroy}. An activity will do all set up of "global" state in onCreate(), andrelease all remaining resources in onDestroy(). For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().

活动的整个生命周期发生在第一次调用```{@link android.app.Activity#onCreate}```到最后一次调用```{@link android.app.Activity#onDestroy}```之间。活动将在```onCreate()```中完成所有“全局”状态设置, 在```Destroy()```中的```释放所有剩余资源```。例如,如果它有一个在后台运行的线程从网络下载数据,它可以在onCreate()中创建该线程,然后在onDestroy()中停止该线程。
  • 可见的生命周期

    The visible lifetime of an activity happens between a call to {@link android.app.Activity#onStart} until a corresponding call to {@link android.app.Activity#onStop}. During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.

    活动的可见生命周期发生在对{@link android.app.Activity#onStart}的调用之间,直到相应的{@link android.app.Activity#onStop}调用。在此期间,用户可以在屏幕上看到活动,尽管它可能不在前台并与用户交互。在这两种方法之间,您可以维护向用户显示活动所需的资源。例如,您可以在onStart()中注册{@link android.content.BroadcastReceiver}以监视影响UI的更改,并在用户不再看到您正在显示的内容时在onStop()中取消注册。 onStart()和onStop()方法可以多次调用,因为活动对用户可见并隐藏。

    • 前台生命周期

    The foreground lifetime of an activity happens between a call to {@link android.app.Activity#onResume} until a corresponding call to {@link android.app.Activity#onPause}. During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states – for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered – so the code in these methods should be fairly lightweight.

    活动处于前台生命周期发生在对{@link android.app.Activity#onResume}的调用之间,直到相应的{@link android.app.Activity#onPause}调用。在此期间,活动位于所有其他活动之前并与用户交互。活动可以经常在恢复和暂停状态之间进行 - 例如,当设备进入休眠状态,传递活动结果时,何时传递新意图 - 因此这些方法中的代码应该相当轻量级。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值