zoukankan      html  css  js  c++  java
  • 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(重启之后仍然保存状态)。

  • 相关阅读:
    repair table
    利用逻辑备份恢复部分库表
    Web框架理解
    BootStrape基础使用
    jQuery入门
    BOM操作
    DOM操作
    day12 css样式
    JavaScript基础
    day11 前端知识简单总结
  • 原文地址:https://www.cnblogs.com/larrylawrence/p/5441753.html
Copyright © 2011-2022 走看看