一、onSaveInstanceState

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).

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.

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 (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)).

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

tip:

In situations where the system needs more memory it may kill paused processes to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In general onSaveInstanceState(Bundle) is used to save per-instance state in the activity and onStop() is used to store global persistent data (in content providers, files, etc.)

二、onRestoreInstanceState

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

onRestoreInstanceState() is called only when recreating activity after it was killed by the OS. Such situation happen when:

  • orientation of the device changes (your activity is destroyed and recreated)
  • there is another activity in front of yours and at some point the OS kills your activity in order to free memory (for example). Next time when you start your activity onRestoreInstanceState() will be called.

In contrast: if you are in your activity and you hit Back button on the device, your activity is finish()ed (i.e. think of it as exiting desktop application) and next time you start your app it is started "fresh", i.e. without saved state because you intentionally exited it when you hit Back.

Other source of confusion is that when an app loses focus to another app onSaveInstanceState() is called but when you navigate back to your app onRestoreInstanceState() may not be called. This is the case described in the original question, i.e. if your activity was NOT killed during the period when other activity was in front onRestoreInstanceState() will NOT be called because your activity is pretty much "alive".

三、案例实验

  • 第一次进入应用

    02-09 22:47:39.260: I/System.out(2991): ActivityA onCreate
    02-09 22:47:39.610: I/System.out(2991): ActivityA onStart
    02-09 22:47:39.610: I/System.out(2991): ActivityA onResume

  • 自动灭屏(锁屏)

    02-09 22:47:39.630: I/System.out(2991): ActivityA onPause
    02-09 22:47:39.770: I/System.out(2991): ActivityA onSaveInstanceState
    02-09 22:47:39.770: I/System.out(2991): ActivityA onStop

  • 按POWER键亮屏

    02-09 22:47:50.100: I/System.out(2991): ActivityA onRestart
    02-09 22:47:50.100: I/System.out(2991): ActivityA onStart
    02-09 22:47:50.140: I/System.out(2991): ActivityA onResume

  • 按POWER键灭屏

    02-09 22:49:40.860: I/System.out(2991): ActivityA onPause
    02-09 22:49:40.870: I/System.out(2991): ActivityA onSaveInstanceState
    02-09 22:49:40.870: I/System.out(2991): ActivityA onStop

  • 按BACK键退出

    02-09 22:50:14.650: I/System.out(2991): ActivityA onPause
    02-09 22:50:15.650: I/System.out(2991): ActivityA onStop
    02-09 22:50:15.650: I/System.out(2991): ActivityA onDestroy

  • 按HOME键回到launcher

    02-09 22:51:25.420: I/System.out(2991): ActivityA onPause
    02-09 22:51:26.090: I/System.out(2991): ActivityA onSaveInstanceState
    02-09 22:51:26.090: I/System.out(2991): ActivityA onStop

  • 长按HOME键点击最近使用的程序回到应用

    02-09 22:51:48.910: I/System.out(2991): ActivityA onRestart
    02-09 22:51:48.910: I/System.out(2991): ActivityA onStart
    02-09 22:51:48.910: I/System.out(2991): ActivityA onResume

  • 从ActivityA中点击进入ActivityB

    02-09 22:53:42.170: I/System.out(3158): ActivityA onPause
    02-09 22:53:42.210: I/System.out(3158): ActivityB onCreate
    02-09 22:53:42.670: I/System.out(3158): ActivityA onSaveInstanceState
    02-09 22:53:42.670: I/System.out(3158): ActivityA onStop

  • 从ActivityB按BACK键退回ActivityA

    02-09 22:54:49.320: I/System.out(3158): ActivityA onRestart
    02-09 22:54:49.320: I/System.out(3158): ActivityA onStart
    02-09 22:54:49.320: I/System.out(3158): ActivityA onResume

  • 在ActivityA中点击执行finish()方法

    02-09 22:55:28.110: I/System.out(3158): ActivityA onPause
    02-09 22:55:28.700: I/System.out(3158): ActivityA onStop
    02-09 22:55:28.700: I/System.out(3158): ActivityA onDestroy

  • 旋转屏幕

    02-09 22:59:28.030: I/System.out(3158): ActivityA onPause
    02-09 22:59:28.030: I/System.out(3158): ActivityA onSaveInstanceState
    02-09 22:59:28.030: I/System.out(3158): ActivityA onStop
    02-09 22:59:28.030: I/System.out(3158): ActivityA onDestroy
    02-09 22:59:28.050: I/System.out(3158): ActivityA onCreate
    02-09 22:59:28.220: I/System.out(3158): ActivityA onStart
    02-09 22:59:28.220: I/System.out(3158): ActivityA onRestoreInstanceState
    02-09 22:59:28.230: I/System.out(3158): ActivityA onResume

  • 在ActivityA中点击执行System.exit(0)
    no log

四、总结

onSaveInstanceState用于保存与界面有关的属性以便回到下次该界面时恢复状态,在当前Activity非“自愿”退出时调用,如GC回收内存、按HOME键 ,而BACK键与finish方法属于正常的退出界面的方法;而onRestoreInstanceState出现的场景更加苛刻,只有非手动直接造成Activity退出(暂时理解为执行了onDestory,有不对之处请指出),如旋转屏幕时、GC回收内存后,再次进入该Activity时,会调用该方法。

onSaveInstanceState & onRestoreInstanceState的更多相关文章

  1. 保存现场数据和状态:onSaveInstanceState\onRestoreInstanceState\onCreate()

    当某个activity变得“容易”被系统销毁时,该activity的onSaveInstanceState就会被执行,除非该activity是被用户主动销毁的,例如当用户按BACK键的时候. 注意上面 ...

  2. 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState

    Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...

  3. activity状态的保存和保持(onRetainNonConfigurationInstance和getLastNonConfigurationInstanc

    本文转载于:http://chengbs.iteye.com/blog/1156167 比较onsaveinstancestate() 与 onretainnonconfigurationinstan ...

  4. 5、四大组件之一-Activity与Intent

    一.Activity的定义及作用 1)官方定义:Activity是Android应用程序提供交互界面的一个重要组件 . 也是Android最重要的组件之一 2)Activity是业务类 , 是承载应用 ...

  5. 【转】Android Activity和Intent机制学习笔记----不错

    原文网址:http://www.cnblogs.com/feisky/archive/2010/01/16/1649081.html Activity Android中,Activity是所有程序的根 ...

  6. Android Activity和Intent机制学习笔记

    转自 http://www.cnblogs.com/feisky: Activity Android中,Activity是所有程序的根本,所有程序的流程都运行在Activity之中,Activity具 ...

  7. Android Intent机制与常见的用法

    Activity Android于.Activity所有的程序都是必不可少,程都执行在Activity之中.Activity具有自己的生命周期(见http://www.cnblogs.com/feis ...

  8. 知识树杂谈Android面试(3)

    一.Activity生命周期? a. Activity四种状态? Running.Paused(透明无焦点).Stopped.killed. b. OnStart() OnRusume区分? 是否可以 ...

  9. Android总结篇系列:Activity中几个主要函数详解

    Activity作为Android系统中四大基本组件之一,包含大量的与其他的各大组件.intent.widget以及系统各项服务等之间的交互的函数.在此,本文主要选取实际项目开发中常用的,但完全理解又 ...

随机推荐

  1. Android 多点触控与简单手势(一)

    现在一般的Android手机都会使用电容触摸屏最少可以支持两点触摸,多的可能是七八个,所以基本上都会支持多点触控, android系统中应用程序可以使用多点触控的事件来完成各种手势和场景需求. And ...

  2. 用于列出选项的Windows窗体控件

    可以提供选项列表的控件有ListBox.ComboBox.CheckedListBox,如何正确的使用和选择这些控件,下面对此进行讨论.首先对这三种控件的功能分别进行说明: ListBox ListB ...

  3. window下乌龟git安装和使用

    一.安装git for windows 首先下载git for windows客户端http://msysgit.github.io/ 安装过程没什么特别的,不停next就ok了 图太多就不继续了~~ ...

  4. EntityFramework基础

    好久没有学习新东西了,最近研究了下EntityFramework,将基础代码贴出来, Entity Framework 利用了抽象化数据结构的方式,将每个数据库对象都转换成应用程序对象 (entity ...

  5. 【转】对抗拖库 ―― Web 前端慢加密

    0×00 前言 天下武功,唯快不破.但密码加密不同.算法越快,越容易破. 0×01 暴力破解 密码破解,就是把加密后的密码还原成明文密码.似乎有不少方法,但最终都得走一条路:暴力穷举.也许你会说还可以 ...

  6. JAVA的Random类[转]

    在实际的项目开发过程中,经常需要产生一些随机数值,例如网站登录中的校验数字等,或者需要以一定的几率实现某种效果,例如游戏程序中的物品掉落等. 在Java API中,在java.util包中专门提供了一 ...

  7. imp导入oracle的dmp备份数据

    imp system/oracle fromuser=lc0029999 touser=lc0029999 rows=y commit=y buffer=65536 feedback=10000 ig ...

  8. CAP理论(转)

    add by zhj: CAP理论可以简单的理解为一致性,可用性,可分区性,这三者没有办法同时满足.我们使用的关系型数据库,比如MySQL,Postgresql是CA类型, 而Redis,MongoD ...

  9. Installshield 打包安装包心得

     制作简单的安装软件 声明:下面的教程,是把读者当做完全没接触过IS的角度来制作的. 1. 启动InstallShield 12.建立一个InstallShield MSI Project,如图: 2 ...

  10. PostgreSQL用户角色及其属性介绍

    1.CREATE ROLE创建的用户默认不带LOGIN属性,而CREATE USER创建的用户默认带有LOGIN属性,如下: postgres=# CREATE ROLE pg_test_user_1 ...