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 ...
随机推荐
- MIP启发式算法:local branching
*本文主要是记录并分享最近学习到的知识,算不上原创 *参考文献见链接 本文主要是讲述local branching算法,主要以M. Fischetti的论文 “Local braching”和Pier ...
- HashMap的C++实现
#include <iostream> #include <cstring> #include <string> typedef unsigned int SIZE ...
- NHibernate N+1问题实例分析和优化
1.问题的缘起 考察下面的类结构定义 public class Category { string _id; Category _parent; IList<Category> _chil ...
- [python IO学习篇] [打开包含中文路径的文件]
https://my.oschina.net/mcyang000/blog/289460 打开路径含有中文的文件时,要注意: 1 在windows对文件名编码是采用gbk等进行编码保存,所以要将文 ...
- 九度oj 题目1080:进制转换
题目描述: 将M进制的数X转换为N进制的数输出. 输入: 输入的第一行包括两个整数:M和N(2<=M,N<=36). 下面的一行输入一个数X,X是M进制的数,现在要求你将M进制的数X转换成 ...
- 找不到css 路径问题
今天进入一个网页的时候,一直报找不到css的错误,但是我的路径原来是正确的 后来找网络控制台,发现路径上多了一个listCommodity,是我controller里面的跳到这个页面的路径 然后我在& ...
- Python Excel导入数据库
import xlrd import MySQLdb def inMySQL(file_name): wb = xlrd.open_workbook(file_name) sh = wb.sheet_ ...
- 【bzoj2400】Spoj 839 Optimal Marks 网络流最小割
题目描述 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. 给你一个有n个结点m条边的无向图.其中的一些点的值是给定的,而其余的点的值由你 ...
- CS231n笔记 Lecture 3 Loss Functions and Optimization
这一讲总体上就是引入Loss Function的概念,以及让大家对优化有一个初步的认识,和其他课程里面说的内容大同小异. Loss function Multiclass svm loss multi ...
- Topcoder SRMCards ——贪心
选择一个数x会删去x+1和x-1,问可以最多选多少次. 显然,对于一段连续的数列,贪心的从左向右选取是最优的. 然后就可以贪心的统计答案了. #include <map> #include ...