一。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重要函数的更多相关文章

  1. 安卓开发之activity详解(sumzom)

    app中,一个activity通常是指的一个单独的屏幕,相当于网站里面的一个网页,它是对用户可见的,它上面可以显示一些控件,并且可以监听处理用户的时间做出响应. 那么activity之间如何进行通信呢 ...

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

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8201936 在前文中,我们简要介绍了Andro ...

  3. unity 调用android函数

    unity 调用android函数 分类: unity2013-12-19 17:54 475人阅读 评论(0) 收藏 举报 unityandroidjar 我们知道,安卓project都有一个And ...

  4. Activity启动流程

    Activity启动过程中做了哪些事情?下面的时序图展示里启动过程中函数的调用过程, 从图中可以知道大概流程. 在介绍细节的时候是从上往下函数调用过程介绍的,如果不知道某个函数是在哪里被谁调用的,可以 ...

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

    1 Android应用图层 一直觉得有关DecorView还是有些问题没有搞清楚,今天在看了一点有关SurfaceFlinger的内容以后,顿时突发奇想,想到之前的问题,之前的思考是: 虽然可以将De ...

  6. Activity启动过程源代码分析

    事实上写分析源代码文章总会显得非常复杂非常乏味,可是梳理自己看源代码时的一些总结也是一种提高. 这篇博客分析下Activity启动过程源代码,我会尽量说得简单点. 个人的观点是看源代码不能看得太细,否 ...

  7. Android获取位置信息的方法总结

    1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位.移动通信.导航等多种技术,提供与空间位置相关的综 ...

  8. Android使用Application的好处

    如果一个应用程序有2个入口的,1个入口程序打开修改其中的内容,怎么实现另一个入口的数据也修改呢? 下面就用到Application来实现数据的共享,因为一个应用程序只有一个Application,Ap ...

  9. Android Fragment的使用

    定义 Fragment可以理解成一个迷你型的活动,同样可以包含布局,同样有自己的生命周期.比Activity要轻量级,在程序内部做界面跳转要比Activity快的多. 静态添加 Fragment可以静 ...

随机推荐

  1. 用Emit技术替代反射

    之前在上篇博客说到用表达式来替代反射机制,可以获得较高的性能提升.这篇我们来说说用Emit技术来替代反射. System.Reflection.Emit命名空间类可用于动态发出Microsoft中间语 ...

  2. Python技术公众号100天了

    公众号100天了,是个值得一提的日子! 我从2017年10月31日开始做这个公众号,到今天2018年2月7日,差不多100天时间 .虽然公众号很早就申请了,但直到去年10月31日,我才有真正把这个公众 ...

  3. 一些I2S的基础概念

    在数字音频Datasheet中,我们经常看到256FS,384FS,32kHz,44.1kHz MCLK等概念.一般在数字音频芯片用3个pin作为通讯接口:BCLK,ADCLRC,DOUT.现在做个总 ...

  4. Python归并排序(递归实现)

    为什么归并排序如此有用?1. 快捷和稳定归并排序成为⼀一个非常棒的排序算法主要是因为它的快捷和稳定.它的复杂度即使在最差情况下都是O(n log n).而快速排序在最差情况下的复杂度是O(n^2),当 ...

  5. java中的static方法和实例方法区别

    1.static方法是大家共享的资源,放在内存堆中,比如村里的河水,每个人都可以取,而且不管你创建多少个实例,该方法在内存中只有一个,节省内存空间, 而且访问速度也是比较快的. 2.实例方法就不同,它 ...

  6. volatile关键字比较好的解释

    http://www.tuicool.com/articles/IRvUJbN http://tengj.top/2016/05/06/threadvolatile4/?utm_source=tuic ...

  7. linux 一行一行的读取文件

    #!/bin/bash #reading data from a file count= cat test1 | while read line do echo "Line $count: ...

  8. 2017-5-14 湘潭市赛 Longest Common Subsequence 想法题

    Longest Common Subsequence Accepted : Submit : Time Limit : MS Memory Limit : KB Longest Common Subs ...

  9. DBCP与C3P0数据库连接池

    数据库连接池是做什么的? 学过计算机网络的都知道,在一个内部局域网中.大部分用的都是私有地址,要想和外部 打交道,必须要有相应的合法外部地址相相应.然而内部用户数量巨大.一台机子一个外部IP 是不现实 ...

  10. 机器学习算法-Adaboost

    本章内容 组合类似的分类器来提高分类性能 应用AdaBoost算法 处理非均衡分类问题 主题:利用AdaBoost元算法提高分类性能 1.基于数据集多重抽样的分类器 - AdaBoost 长处 泛化错 ...