super.onCreate(savedInstanceState) 以及onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
super.onCreate(savedInstanceState)
调用父类的onCreate构造函数。
当一个Activity在生命周期结束前,会调用onSaveInsanceState()这个回调函数来保存状态。保存类型是Bundle类型。如下:public void onSaveInsanceState(Bundle saveInsanceState){
super.onSaveInsanceState(saveInsanceState);
}
然后,当一个Activity被创建时,就能从onCreate的参数saveInsanceState中获得状态数据。所以电子书、游戏之类的有进度的程序在异常退出之前,可以复写onSaveInsanceState()来保存进度;然后当次打开的时候,onRestoreInstanceState() and onCreate()会接收到savedInstanceState这个参数,然后就可以调用:
if(null != savedInstanceState)
....
来读取上次的进度。
示例:
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);
}
}
但是,我还是不太懂为什么要调用父类的onCreate()方法。而且我发现网上的人大多也不懂。。比如下面这个帖子:
http://bbs.csdn.net/topics/390509790
回答的人跟我一样陷入了解答savedInstanceState作用的自嗨中,根本没有回答题主的问题,为什么父类没有使用穿过去的savedInstance参数。
这让我想起昨天看到的一个视频http://v.youku.com/v_show/id_XNjI1MTc2MTA4.html?from=s1.8-1-1.2?tpa=dW5pb25faWQ9MjAwMDAxXzEwMDEyNl8wMV8wNQ,13:55的时候老师直播被学生打脸,老师自己都不知道webview的启动模式是第三方浏览器,场面尴尬,而这个老师瞬间转移话题到网页上的内容上去,场面更加尴尬了.
这里我引用一篇http://stackoverflow.com/questions/14671897/super-oncreatesavedinstancestate上的回答,作者从super角度回答了这个问题。为什么我们要调用super.onCreate()。
Every Activity you make is started through a sequence of method calls. onCreate() is the first of these calls.
Each and every one of your Activities extends android.app.Activity either directly or by subclassing another subclass of Activity.
In Java, when you inherit from a class, you can override its methods to run your own code in them. A very common example of this is the overriding of the toString() method when extending java.lang.Object.
When we override a method, we have the option of completely replacing the method in our class, or of extending the existing parent class' method. By calling super.onCreate(savedInstanceState);, you tell the Dalvik VM to run your code in addition to the existing code in the onCreate() of the parent class. If you leave out this line, then only your code is run. The existing code is ignored completely.
However, you must include this super call in your method, because if you don't then the onCreate() code in Activity is never run, and your app will run into all sorts of problem like having no Context assigned to the Activity (though you'll hit a SuperNotCalledException before you have a chance to figure out that you have no context).
In short, Android's own classes can be incredibly complex. The code in the framework classes handles stuff like UI drawing, house cleaning and maintaining the Activity and application lifecycles. super calls allow developers to run this complex code behind the scenes, while still providing a good level of abstraction for our own apps.
如他所说,Android's own classes can be incredibly complex,我们用super,只是在复写onCreate时候在父类的onCreate执行复杂操作的时候,增加一些我们的额外的边边角角的工作。
可以看我很早以前写的关于复写的随笔:http://www.cnblogs.com/larrylawrence/p/3519751.html,复写的意义很大程度上是追加,而不是覆盖。
onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
See:http://developer.android.com/reference/android/app/Activity.html)
onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
Same as onCreate(android.os.Bundle) but called for those activities created with the attribute persistableMode set to persistAcrossReboots.
需要persistableMode(或许解释了为什么一般的Activity调用了带这个参数的onCreate之后出现一片空白)。它可以实现persistAcrossReboots(重启之后仍然保存状态)。
super.onCreate(savedInstanceState) 以及onCreate(Bundle savedInstanceState, PersistableBundle persistentState)的更多相关文章
- 关于onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
API 21为Activity增加了一个新的属性,只要将其设置成persistAcrossReboots,activity就有了持久化的能力,另外需要配合一个新的bundle才行,那就是Persist ...
- android Bundle savedInstanceState用途
经常会出现用户按到home键,退出了界面,或者安卓系统意外回收了应用的进程,这种情况下,使用Bundle savedInstanceState就可以用户再次打开应用的时候恢复的原来的状态 (以下转自: ...
- Bundle savedInstanceState的作用
写过Android程序的都知道Activity中有一个名称叫onCreate的方法.该方法是在Activity创建时被系统调用,是一个Activity生命周期的开始.可是有一点容易被忽视,就是onCr ...
- Android——Bundle savedInstanceState的作用
写过Android程序的都知道Activity中有一个名称叫onCreate的方法.该方法是在Activity创建时被系统调用,是一个Activity生命周期的开始.可是有一点容易被忽视,就是onCr ...
- onCreate不加载布局
如果Activity重写的是 onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentS ...
- Android创建窗口(一)创建应用窗口
所谓的窗口(Window)就是一个显示在手机屏幕上可视化视图的一片区域.在Android中窗口是一个抽象的概念,每一个Activity就对应着一个窗口,而所有的窗口都是由视图(View)来呈现,而我们 ...
- Android-消息处理学习总结(Handler,Looper)
参考资料: http://www.cnblogs.com/qlky/p/5657924.html http://blog.csdn.net/guolin_blog/article/details/99 ...
- Activity启动过程源码分析(Android 8.0)
Activity启动过程源码分析 本文来Activity的启动流程,一般我们都是通过startActivity或startActivityForResult来启动目标activity,那么我们就由此出 ...
- 【Android开发艺术探索】四大组件的工作过程
个人博客 http://www.milovetingting.cn 四大组件的工作过程 四大组件:Activity.Service.BroadcastReceiver.ContentProvider ...
随机推荐
- Oracle 优化器
http://blog.csdn.net/it_man/article/details/8185370一.优化器基本知识 Oracle在执行一个SQL之前,首先要分析一下语句的执行计划,然后再按执 ...
- iOS常用的加密方式
MD5 iOS代码加密 创建MD5类,代码如下 #import <Foundation/Foundation.h> @interface CJMD5 : NSObject +(NSStri ...
- [jjzhu学java]之solr4.9同步mysql数据
Solr是一个高性能,採用Java5开发,基于Lucene的全文搜索server.同一时候对其进行了扩展,提供了比Lucene更为丰富的查询语言,同一时候实现了可配置.可扩展并对查询性能进行了优化,而 ...
- Volley框架载入网络图片
Android开发中,载入网络server的图片是非经常常使用的.当然我们能够自己写server接口去实现,只是要做到server性能 优越的话,开发起来比較麻烦点.所以本博客要介绍Volley框架进 ...
- zabbix-agent active 配置自动探测
1. zabbix-agent 被动模式配置文件: PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_a ...
- 一些编译php时的configure 参数
一些编译php时的configure 参数 ./configure –prefix=/usr/local/php php 安装目录 –with-apxs2=/usr/local/apache/bin/ ...
- flex做页面。用来做视频的后台服务器是fms
作为新一代的富客户端互联网技术的佼佼者,Flex这种技术已经被越来越多的公司所采用,被越来越多的用户和程序员所接受.以下列出Flex十大优势: 1.Flex与Flash:可以让普通程序员开发制作Fla ...
- 【python】python版本升级,从2.6.6升级到2.7.13
centos6.5系统自带了2.6.6版本的python,有时候为了项目上的需要,需要将python版本升级到2.7.13,下面介绍了如何进行升级. 说明:python从2.6升级到2.7会引发很多问 ...
- java.util.ResourceBundle国际化用法详解
java.util.ResourceBundle国际化用法详解 初识国际化和ResourceBundle 这个类主要用来解决国际化和本地化问题.国际化和本地化可不是两个概念,两者都是一起出现的.可以说 ...
- html--<meta>设置缓存
html头文件设置常用之<meta>设置缓存 <meta http-equiv="pragma" content="no-cache"&g ...