• 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)的更多相关文章

  1. 关于onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

    API 21为Activity增加了一个新的属性,只要将其设置成persistAcrossReboots,activity就有了持久化的能力,另外需要配合一个新的bundle才行,那就是Persist ...

  2. android Bundle savedInstanceState用途

    经常会出现用户按到home键,退出了界面,或者安卓系统意外回收了应用的进程,这种情况下,使用Bundle savedInstanceState就可以用户再次打开应用的时候恢复的原来的状态 (以下转自: ...

  3. Bundle savedInstanceState的作用

    写过Android程序的都知道Activity中有一个名称叫onCreate的方法.该方法是在Activity创建时被系统调用,是一个Activity生命周期的开始.可是有一点容易被忽视,就是onCr ...

  4. Android——Bundle savedInstanceState的作用

    写过Android程序的都知道Activity中有一个名称叫onCreate的方法.该方法是在Activity创建时被系统调用,是一个Activity生命周期的开始.可是有一点容易被忽视,就是onCr ...

  5. onCreate不加载布局

    如果Activity重写的是 onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentS ...

  6. Android创建窗口(一)创建应用窗口

    所谓的窗口(Window)就是一个显示在手机屏幕上可视化视图的一片区域.在Android中窗口是一个抽象的概念,每一个Activity就对应着一个窗口,而所有的窗口都是由视图(View)来呈现,而我们 ...

  7. Android-消息处理学习总结(Handler,Looper)

    参考资料: http://www.cnblogs.com/qlky/p/5657924.html http://blog.csdn.net/guolin_blog/article/details/99 ...

  8. Activity启动过程源码分析(Android 8.0)

    Activity启动过程源码分析 本文来Activity的启动流程,一般我们都是通过startActivity或startActivityForResult来启动目标activity,那么我们就由此出 ...

  9. 【Android开发艺术探索】四大组件的工作过程

    个人博客 http://www.milovetingting.cn 四大组件的工作过程 四大组件:Activity.Service.BroadcastReceiver.ContentProvider ...

随机推荐

  1. EAI G4-lidar ROS配置

    (1)使用命令创建 ydlidar_ws 工作空间,并将 G4 资料包内的 ROS 驱动包 ydlidar 下载到ydlidar_ws/src 目录下,切换到 ydlidar_ws 工作空间下并重新进 ...

  2. nginx for windows 配置多域名反向代理

    调试了很久...哦耶 共享出来吧 其实 nginx反向代理同一ip多个域名,给header加上host就可以了 upstream test.test.cn {        server   119. ...

  3. Cookie的写入,和读取

    public static void SetLoginGmameInfo(string  uid, string sid, string timestring, string sign)       ...

  4. CSU - 1556 Jerry's trouble(高速幂取模)

    [题目链接]:click here [题目大意]:计算x1^m+x2^m+..xn^m(1<=x1<=n)( 1 <= n < 1 000 000, 1 <= m < ...

  5. python3.x中xml.etree.ElementTree解析xml举例

    1.新建xml import xml.etree.ElementTree as ETa=ET.Element('elem')c=ET.SubElement(a,'child1')c.text=&quo ...

  6. Cesium--气泡弹窗

    参考资料 首先感谢以下博主们的帮助,本人刚接触Cesium不久,无奈只能拾人牙慧了. 由于cesium没有自带的点击弹出气泡的功能,所以需要自己去开发一个这样的功能,网络上资源很多,看到基本思路都一致 ...

  7. js jquery 插件

    $(function(){ (function($, document, undefiend){ $.fn.pagination = function(options){ var $this = $( ...

  8. maven nexus 搭建

    http://www.cnblogs.com/adolfmc/archive/2012/08/21/2648415.html http://www.cnblogs.com/dingyingsi/p/3 ...

  9. eclipse tomcat maven

    jdk jre eclipse 略过 下载maven和tomcat 上apache官网下载maven:http://maven.apache.org/download.cgi. 上apache官网下载 ...

  10. hadoop基础----hadoop理论(四)-----hadoop分布式并行计算模型MapReduce具体解释

    我们在前一章已经学习了HDFS: hadoop基础----hadoop理论(三)-----hadoop分布式文件系统HDFS详细解释 我们已经知道Hadoop=HDFS(文件系统,数据存储技术相关)+ ...