Activity重要函数

一。onConfigurationChanged 与 android:configChanges

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut

down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted.

Instead, the activity remains running and its onConfigurationChanged() method is called.

配置了android:configChanges后,当声明的configChange改变时,系统会调用Activity的onConfigurationChanged,而不会重新创建Activity.

android:configChanges="orientation|keyboardHidden|screenSize"

HONEYCOMB_MR2(3.2以上)

二。onRetainNonConfigurationInstance 与 getLastNonConfigurationInstance

This function is called purely as an optimization, and you must not rely on it being called. When it is called, a number of guarantees

will be made to help optimize configuration switching:

  • The function will be called between onStop() and onDestroy().
  • A new instance of the activity will always be immediately created after this one‘s onDestroy() is called. In particular, no messages

will be dispatched during this time (when the returned object does not have an activity to be associated with).

  • The object you return here will always be available from the getLastNonConfigurationInstance() method of the following activity

instance as described there.

这个方法最大的好处是: 
    * 当Activity曾经通过某个资源得到一些图片或者信息,那么当再次恢复后,无需重新通过原始资源地址获取,可以快速的加载整个Activity状态信息。

* 当Activity包含有许多线程时,在变化后依然可以持有原有线程,无需通过重新创建进程恢复原有状态。

* 当Activity包含某些Connection Instance时,同样可以在整个变化过程中保持连接状态。

注意: 
    * onRetainNonConfigurationInstance()在onSaveInstanceState()之后被调用。

* 调用顺序同样介于onStop() 和 onDestroy()之间。

三。Fragment 的 setRetainInstance

Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be

used with fragments not in the back stack. If set, the fragment lifecycle will be slightly different when an activity is recreated:

  • onDestroy() will not be called (but onDetach() still will be, because the fragment is being detached from its current activity).
  • onCreate(Bundle) will not be called since the fragment is not being re-created.
  • onAttach(Activity) and onActivityCreated(Bundle)will still be called.

四。onSaveInstanceState 与 onRestoreInstanceState

This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state.

For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will

have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state

of the user interface can be restored via onCreate(Bundle) oronRestoreInstanceState(Bundle).

The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in

the hierarchy that has an id, and by saving the id of the currently focused view. If you override this method to save additional

information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be

prepared to save all of the state of each view yourself.

If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().

Activity重要函数

时间: 2024-07-29 16:58:59

Activity重要函数的相关文章

Android中一个Activty控制另一个Activity的函数及变量

http://book.zhulang.com/312241/354401.html http://book.zhulang.com/312241/354402.html http://book.zhulang.com/312241/354403.html http://book.zhulang.com/312241/354404.html http://book.zhulang.com/312241/354405.html http://book.zhulang.com/312241/3544

MonkeyRunner 之如何获取APP的Package Name和Activity Name

MonkeyRunner 之如何获取APP的Package Name和Activity Name   最近尝试学习使用monkeyrunner进行Android自动化测试,一开始均是使用点击屏幕坐标的方法来运行应用程序,可是点击屏幕坐标的方法比较麻烦,且通用性差.通过度娘我知道可以使用device.startActivity("package name /activity")这个函数来直接启用安装在手机中的app,可是如何获得activity却成了关键,现将获取activity的方法总

DroidPlugin源码分析处理Activity的启动211ew4dfr

正常情况下启动一个Activity,首先需要在AndroidManifest文件中声明,其次需要把该应用安装到手机系统中. 而插件apk是没有正在安装到手机系统中的,也就按照正常的启动流程插件Activity是不能启动的.另外插件apk的类需要加载进来是需要指定ClassLoader.前面的文章也大概讲过,当启动一个插件Activity时,先是用预定义的代理Activity替换目标Activity(及插件Activity)去启动,当AMS处理完回调到应用空间时(及回到运行Activity的进程空

DroidPlugin源码分析处理Activity的启动

正常情况下启动一个Activity,首先需要在AndroidManifest文件中声明,其次需要把该应用安装到手机系统中. 而插件apk是没有正在安装到手机系统中的,也就按照正常的启动流程插件Activity是不能启动的.另外插件apk的类需要加载进来是需要指定ClassLoader.前面的文章也大概讲过,当启动一个插件Activity时,先是用预定义的代理Activity替换目标Activity(及插件Activity)去启动,当AMS处理完回调到应用空间时(及回到运行Activity的进程空

安卓开发之activity详解(sumzom)

app中,一个activity通常是指的一个单独的屏幕,相当于网站里面的一个网页,它是对用户可见的,它上面可以显示一些控件,并且可以监听处理用户的时间做出响应. 那么activity之间如何进行通信呢?他们是通过intent进行通信以及切换.Intent有两个重要部分(动作传递,数据传递): ?神马是Activity? activity是四大组件,代表者用户可操作界面,可与用户进行交互.比如,拍照,登陆注册等交互界面.一个应用程序通常由多个activity松耦合(低耦合)组成,当用户打开app见

Activity启动(1)----流程

Activity启动过程中做了哪些事情?下面的时序图展示里启动过程中函数的调用过程, 从图中可以知道大概流程. 在介绍细节的时候是从上往下函数调用过程介绍的,如果不知道某个函数是在哪里被谁调用的,可以回过头来看下时序图.下面是对一些细节进行介绍. 1. 在Android中有两种操作会引发Activity的启动,一种用户点击Launcher的应用程序图标时,Launcher会为启动应用程序的主Activity.另外一种是在已经起来的Activity内部通过调用startActvity接口启动新的A

Android简单的将Activity改为Fragment

开发android有时会遇到这样的问题,那就是需要将Activity改造成Fragment.本来Fragment是新的android版本上的东西,不过既然google这么亲切的提供了向下兼容的包.那么总会有这样的需要的时候,好在Activity到Fragment的转化并不复杂,因为本来就是相似的东西.最近就遇到了这的问题,于是来总结一下经验. 从Activity到Fragment最大的变动在于,在Fragment里面的onCreate()函数内,是没有办法使用findViewById.setCo

Android减少布局层次--有关Activity根视图DecorView的思考

1 前面文章里面总结的一张图 一直觉得有关DecorView还是有些问题没有搞清楚,今天在看了一点有关SurfaceFlinger的内容以后,顿时突发奇想,想到之前的问题,之前的思考是: 虽然可以将DecorView作为Activity布局的父View,也就是只存在  DecorView---->Activity Layout两层,但是经过试验还是会存在Title Bar,或者说是现在的Action Bar,尝试如下: protected void onCreate(Bundle savedIn

Android应用程序窗口(Activity)的运行上下文环境(Context)的创建过程分析

文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8201936 在前文中,我们简要介绍了Android应用程序窗口的框架.Android应用程序窗口在运行的过程中,需要访问一些特定的资源或者类.这些特定的资源或者类构成了Android应用程序的运行上下文环境,Android应用程序窗口可以通过一个Context接口来访问它,这个Context接口也是我们在开发应用程序时经常碰到的.在本文中,我们