Android onCreate 的savedInstanceState 作用
在activity的生命周期中,只要离开了可见阶段,或者说失去了焦点,activity就很可能被进程终止了!,被KILL掉了,,这时候,就需要有种机制,能保存当时的状态,这就是savedInstanceState的作用。
当一个Activity在PAUSE时,被kill之前,它可以调用onSaveInstanceState()来保存当前activity的状态信息(在paused状态时,要被KILLED的时候)。用来保存状态信息的Bundle会同时传给两个method,即onRestoreInstanceState() and onCreate().
示例代码如下:
package com.myandroid.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class AndroidTest extends Activity {
private static final String TAG = "MyNewLog";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If an instance of this activity had previously stopped, we can
// get the original text it started with.
if(null != savedInstanceState)
{
int IntTest = savedInstanceState.getInt("IntTest");
String StrTest = savedInstanceState.getString("StrTest");
Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
}
setContentView(R.layout.main);
Log.e(TAG, "onCreate");
} @Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save away the original text, so we still have it if the activity
// needs to be killed while paused.
savedInstanceState.putInt("IntTest", 0);
savedInstanceState.putString("StrTest", "savedInstanceState test");
super.onSaveInstanceState(savedInstanceState);
Log.e(TAG, "onSaveInstanceState");
} @Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int IntTest = savedInstanceState.getInt("IntTest");
String StrTest = savedInstanceState.getString("StrTest");
Log.e(TAG, "onRestoreInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
}
}
转自:http://blog.csdn.net/xiaoxiao_job/article/details/5852811
Android onCreate 的savedInstanceState 作用的更多相关文章
- super.onCreate(savedInstanceState) 以及onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
super.onCreate(savedInstanceState) 调用父类的onCreate构造函数. 当一个Activity在生命周期结束前,会调用onSaveInsanceState()这个回 ...
- AIDL在android系统中的作用
AIDL,Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口.最近看了下AIDL在A ...
- Android笔记——Application的作用
一.Application是什么 Application和Actovotu,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application对象, ...
- android 中IntentService的作用及使用
IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同 ...
- 关于onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
API 21为Activity增加了一个新的属性,只要将其设置成persistAcrossReboots,activity就有了持久化的能力,另外需要配合一个新的bundle才行,那就是Persist ...
- Android——onCreate( )方法详解(转)
android开发之onCreate( )方法详解 onCreate( )方法是android应用程序中最常见的方法之一,那么,我们在使用onCreate()方法的时候应该注意哪些问题呢? 先看看Go ...
- Android——OnCreate
OnCreate OnCreate是Android中的一个特别的函数,用来“表示一个窗口正在生成”.其不产生窗口,只是在窗口显示前设置窗口的属性如风格.位置等. 性 质 函数 特 点 不产 ...
- 【Android Studio安装部署系列】四、Android SDK目录和作用分析
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 Android SDk Tool软件开发工具包(software development kit).被软件开发工程师用于为特定的软件 ...
- android onCreate的两个方法
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { } override ...
随机推荐
- Android自动化测试如何获取坐标点?
有以下三种方法: 1.打开开发者选项中的“显示指针位置”: 英文文版本为Settings->Developer option->Show touches(on)->Pointer l ...
- day07 类的进阶,socket编程初识
类的静态方法: 正常: 添加静态方法: 就会变成一个函数,不会自动传self 参数,不会调用类的变量和实例的变量 不在需要self 名义上归类管,但是它就是一个单独的函数,不在需要传入self,想怎 ...
- 微信小程序开发 -- 手机振动
wx.vibrateLong(OBJECT) wx.vibrateLong(OBJECT) 方法使手机发生较长时间的振动(400ms) OBJECT参数说明: 参数名 类型 必填 说明 success ...
- 如何安装mongodb.msi
到MongoDB官网下载MongoDB软件:mongodb-win32-x86_64-2008plus-ssl-3.0.2-signed.msi, 放在想要安装的地方: 如:d:\MongoDB\ 2 ...
- 使用 D8 分析 javascript 如何被 V8 引擎优化的
在上一篇文章中我们讲了如何使用 GN 编译 V8 源码,文章最后编译完成的可执行文件并不是 V8,而是 D8.这篇我们讲一下如何使用 D8 调试 javascript 代码. 如果没有 d8,可以使用 ...
- leetcode with python -> tree
100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...
- aiomysql inserting operation failed !
emotions: those days,i am using aiomysql(python3.5) to acess my database .But a 'strange' problem ma ...
- [python][oldboy] * **的用法
* 和**主要用在函数的参数中, # coding=utf8 """ 三种编码: 1 python程序代码的编码 # coding=utf8 2 设置/查看python程 ...
- [笔记]Docker解决了什么问题?
Docker的优势: 环境依赖问题 更轻量的虚拟化,节省了虚拟机的性能损耗 Docker应用场景: 程序分发,gitlab的安装很恶心吧,所以有人做了gitlab的image 部署发布,这点对运维的同 ...
- 【转】深入理解JVM—JVM内存模型
http://www.cnblogs.com/dingyingsi/p/3760447.html#3497199 我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU ...