• 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. unslider点导航不显示错误

    原因是,unslider插件只添加了dots文件并没有设置样式 解决办法 .banner { position: relative; width: 100%; overflow: auto; font ...

  2. caffe-ubuntu1604-gtx850m-i7-4710hq----VGG_ILSVRC_16_layers.caffemodel

    c++调用vgg16: ./build/install/bin/classification \ /media/whale/wsWin10/wsCaffe/model-zoo/VGG16//deplo ...

  3. nyist oj 37 回文字符串 (动态规划经典)

    回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 所谓回文字符串,就是一个字符串.从左到右读和从右到左读是全然一样的.比方"aba".当 ...

  4. C#中方法中 ref 和 out的使用

    案例1: static void Main() { , , , }; int numLargerThan10,numLargerThan100,numLargerThan1000 ; Proc(ary ...

  5. golang生成随机函数的实现

    golang生成随机数可以使用math/rand包, 示例如下: package main import ( "fmt" "math/rand" ) func ...

  6. ios-逆向 手把手安装最新版Theos

      Theos.最初由DHowett进行开发,由于DHwoett去了微软,不再有时间维护了,所以Adam Demasi(kirb)接手了他的工作,并且添加了很多全新的功能.所以,之前书上<iOS ...

  7. COGS1532. [IOI2001]移动电话

    1532. [IOI2001]移动电话 ★☆   输入文件:mobilephones.in   输出文件:mobilephones.out   简单对比时间限制:5 s   内存限制:256 MB [ ...

  8. 【BZOJ4843】[Neerc2016]Expect to Wait 排序

    [BZOJ4843][Neerc2016]Expect to Wait Description ls最近开了一家图书馆,大家听说是ls开的,纷纷过来借书,自然就会出现供不应求的情况, 并且借书的过程类 ...

  9. 网页直播、微信直播技术解决方案:EasyNVR与EasyDSS流媒体服务器组合之区分不同场景下的easynvr

    近期遇到好多客户咨询关于实现微信直播.或者是将直播页面集成进入自己项目中. 该方案的主要目的:完成在公网一直进行内网摄像头的RTMP/HLS直播! 实现方案的具体实现: EasyNVR+EasyDSS ...

  10. 九度OJ 1056:最大公约数 (GCD)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6278 解决:4075 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. 输出: 对于每组输入,请 ...