• 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. 16:Merge

    题目描述 数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出. 输入描述:先输入键值对的个数,然后输入成对的index和value值 ...

  2. 命令行高效操作Git,看这篇就够了

    原文地址:http://blog.jboost.cn/2019/06/16/use-git.html 对于软件开发人员来说,git几乎是每天都需要接触的工具.但对于相处如此亲密的工作伙伴,你对它的了解 ...

  3. java 表示当前时间的第二天的几点

    Calendar cal = Calendar.getInstance();  cal.setTime(new Date());  cal.add(Calendar.DAY_OF_YEAR, 1);  ...

  4. InputFormat的认识

    InputFormat 负责处理MR的输入部分. 有三个作用: 一.验证作业的输入是否规范. 二.把输入文件切分成InputSplit. 三.提供RecordReader 的实现类,把InputSpl ...

  5. java8笔记: sorted()之正序倒序

    java8笔记: sorted()之正序倒序 这篇文章将会讲解Java 8 Stream sorted()示例 下面代码以自然序排序一个list List<Person> listTem ...

  6. TP 框架 如果去掉表前缀

    #jd_admin_abc 去掉前缀 C('DB_PREFIX')=获取前缀 结果为admin_abc $table_Name=str_replace(C('DB_PREFIX'), '', $tab ...

  7. 再看GS接包过程

    再看GS接包过程 bool GameServer::ProcessLoop(packet& rPkt) { if(false == m_spDataLayer->Recv(rPkt)) ...

  8. Git 自己的一些工作中的总结

    这个网址很重要:https://gitee.com/progit/2-Git-%E5%9F%BA%E7%A1%80.html#2.4-%E6%92%A4%E6%B6%88%E6%93%8D%E4%BD ...

  9. 九度OJ 1015:还是A+B (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6773 解决:4031 题目描述: 读入两个小于10000的正整数A和B,计算A+B.需要注意的是:如果A和B的末尾K(不超过8)位数字相同 ...

  10. Bloom Filters

    http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html A Bloom filter is a method for represe ...