首先Android的Activity生命周期如下图:

Activity的onSaveInstanceState()和onRestoreInstanceState()并不是生命周期方法,他们不同于onCreate(),onPause()等生命周期,并不是一定会被触发。当应用遇到意外情况(内存不足,用户直接按HOME键,pause,stop ) 由系统销毁一个Activity时,onSavenstanceState()会被调用,Activity由运行状态进入暂停或者停止状态也会调用该方法。但是当用户主动去销毁一个Activity时,如在应用中按返回键突出,就不会被调用。应为这种情况下用户的行为决定了不需要保存Activity的状态,通常onSaveInstanceState()只适合用于保存一些临时的状态,

onPause()方法适合用于数据的持久化保存

当屏幕方向发生改变,activity会被摧毁并且被重新创建。如果你想在activity被摧毁前缓存一些数据,并且在activity被重新创建后回复缓存数据,可以重写Activity的onSaveInstanceState()和onRestoreInstanceState()两个方法

protected void onRestoreInstanceState(Bundle savedInstanceState) {
  String name = savedInstanceState.getString("name");
  Log.i(TAG,"here is onRestoreInstanceState" + " name=" + name);
  super.onRestoreInstanceState(savedInstanceState);
 }
 protected void onSaveInstanceState(Bundle outState) {
  Log.i(TAG,"here is onSaveInstanceState");
  outState.putString("name", "linxuanming");
  super.onSaveInstanceState(outState);
 }

Activity的onSaveInstanceState()和onRestoreInstanceState()方法的更多相关文章

  1. Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法:

    Android Activity的onSaveInstanceState() 和 onRestoreInstanceState()方法: 1. 基本作用: Activity的 onSaveInstan ...

  2. onSaveInstanceState()和onRestoreInstanceState()方法

    Activity的 onSaveInstanceState() 和 onRestoreInstanceState()并不是生命周期方法,它们不同于 onCreate().onPause()等生命周期方 ...

  3. Activity的onSaveInstanceState和onRestoreInstanceState触发的时机

    转自:http://www.cnblogs.com/heiguy/archive/2010/10/30/1865239.html 1.原文 先看Application Fundamentals上的一段 ...

  4. Activity的onSaveInstanceState()和onRestoreInstanceState()以及API详解

    为了弄清楚onSaveInstanceState()方法和onRestoreInstanceState()方法,我翻译一下谷歌的API,翻译如下: There are a few scenarios ...

  5. Android onSaveInstanceState和onRestoreInstanceState()

    首先来介绍onSaveInstanceState() 和 onRestoreInstanceState() .关于这两个方法,一些朋友可能在Android开发过程中很少用到,但在有时候掌握其用法会帮我 ...

  6. Android Activity生命周期 onSaveInstanceState和onRestoreInstanceState

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

  7. Android 中onSaveInstanceState和onRestoreInstanceState学习

    1. 基本作用: Activity的 onSaveInstanceState() 和 onRestoreInstanceState()并不是生命周期方法,它们不同于 onCreate().onPaus ...

  8. View的onSaveInstanceState和onRestoreInstanceState过程分析

    为什么要介绍这2个方法呢?这是因为在我们的开发中最近遇到了一个很诡异的bug.大体是这样的:在我们的ViewPager中 有2页的root view都是ScrollView,我们在xml里面都用了an ...

  9. onSaveInstanceState() 和 onRestoreInstanceState()

    本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceState(),并且在介绍这两个方法之后,再分别来实现使用 ...

随机推荐

  1. c语言else匹配问题

    #include <stdio.h> #include <stdlib.h> //实现 依次输入三个递增的数 然后正确输出 //为什么得不到我们想要的结果呢 这就是else匹配 ...

  2. RequireJS入门(一)

    RequireJS由James Burke创建,他也是AMD规范的创始人. RequireJS会让你以不同于往常的方式去写JavaScript.你将不再使用script标签在HTML中引入JS文件,以 ...

  3. poj 2046 Gap(bfs+hash)

    Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...

  4. java获取项目地址或tomcat绝对地址

    在java项目中获取文件的路径,不管是相对路径还是绝对路径,其本质都是通过绝对路径去寻找. 获取项目地址 request.getSession().getServletContext().getRea ...

  5. maven-Android项目环境搭建

    参考:http://blog.csdn.net/earbao/article/details/40741051 android maven环境搭建: 1.Maven的版本要求3.1.1 2.设置AND ...

  6. (转)Building MariaDB on Mac OS X using Homebrew

    https://kb.askmonty.org/en/building-mariadb-on-mac-os-x-using-homebrew/ Work has been done to provid ...

  7. input文本框获取焦点和失去焦点判断

    onBlur:当输入框失去焦点后 onFocus:当输入框获得焦点后 这两个JavaScript事件是写在html标签中的例如: <input type="text" onB ...

  8. [RxJS] Toggle A Stream On And Off With RxJS

    This lesson covers how to toggle an observable on and off from another observable by showing how to ...

  9. HDU 多校联合练习赛2 Warm up 2 二分图匹配

    Warm up 2 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

  10. ubuntu16.04 server安装小记

    由于本人有一台闲置的thinkpad电脑,所以打算在上边安装一个ubuntu16.04 server版本,其中遇到主要问题,做一下记录: 安装过程中出现“ubuntu16.04 server64 bu ...