`
闫老三
  • 浏览: 102731 次
社区版块
存档分类
最新评论

【引用】android onCreate(Bundle savedInstanceState)

 
阅读更多

写过Android程序的都知道Activity中有一个名称叫onCreate的方法。该方法是在Activity创建时被系统调用,是一个Activity生命周期的开始。可是有一点容易被忽视,就是onCreate方法的参数saveInsanceState。因为在一般的程序开发中,很少用到这个参数。
onCreate方法的完整定义如下:

public void onCreate(Bundle saveInsanceState){
super.onCreate(saveInsanceState);
}

 上面的代码可以看出,onCreate方法的参数是一个Bundle类型的参数。Bundle类型的数据与Map类型的数据相似,都是以key-value的形式存储数据的。

    从字面上看saveInsanceState,是保存实例状态的。实际上,saveInsanceState也就是保存Activity的状态的。那么,saveInsanceState中的状态数据是从何处而来的呢?下面我们介绍Activity的另一个方法saveInsanceState。
    onsaveInsanceState方法是用来保存Activity的状态的。当一个Activity在生命周期结束前,会调用该方法保存状态。这个方法有一个参数名称与onCreate方法参数名称相同。如下所示:

public void onSaveInsanceState(Bundle saveInsanceState){
super.onSaveInsanceState(saveInsanceState);
}

  在实际应用中,当一个Activity结束前,如果需要保存状态,就在onsaveInsanceState中,将状态数据以key-value的形式放入到saveInsanceState中。这样,当一个Activity被创建时,就能从onCreate的参数saveInsanceState中获得状态数据。

    状态这个参数在实现应用中有很大的用途,比如:一个游戏在退出前,保存一下当前游戏运行的状态,当下次开启时能接着上次的继续玩下去。再比如:电子书程序,当一本小说被阅读到第199页后退出了(不管是内存不足还是用户自动关闭程序),当下次打开时,读者可能已忘记了上次已阅读到第几页了,但是,读者想接着上次的读下去。如果采用saveInstallState参数,就很容易解决上述问题。 
简单的事例api中snake游戏 在SnakeView类中

private int[] coordArrayListToArray(ArrayList cvec) {
        int count = cvec.size();
        int[] rawArray = new int[count * 2];
        for (int index = 0; index < count; index++) {
            Coordinate c = cvec.get(index);
            rawArray[2 * index] = c.x;
            rawArray[2 * index + 1] = c.y;
        }
        return rawArray;
    }
    
    public Bundle saveState() {
        Bundle map = new Bundle();
        map.putIntArray("mAppleList", coordArrayListToArray(mAppleList));
        map.putInt("mDirection", Integer.valueOf(mDirection));
        map.putInt("mNextDirection", Integer.valueOf(mNextDirection));
        map.putLong("mMoveDelay", Long.valueOf(mMoveDelay));
        map.putLong("mScore", Long.valueOf(mScore));
        map.putIntArray("mSnakeTrail", coordArrayListToArray(mSnakeTrail));
        return map;
    }
在snakeActivity中实现
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.snake_layout);
        mSnakeView = (SnakeView) findViewById(R.id.snake);
        mSnakeView.setTextView((TextView) findViewById(R.id.text));
        if (savedInstanceState == null) {
            // We were just launched -- set up a new game
            mSnakeView.setMode(SnakeView.READY);
        } else {
            // We are being restored
            Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
            if (map != null) {
                mSnakeView.restoreState(map);
            } else {
                mSnakeView.setMode(SnakeView.PAUSE);
            }
        }
    }
并重写onSavedInstanceState(),此方法会在activity结束时,调用.
    @Override
    public void onSaveInstanceState(Bundle outState) {
        //Store the game state
        outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
    }
分享到:
评论

相关推荐

    PinnedSectionListview

    protected void onCreate Bundle savedInstanceState { super onCreate savedInstanceState ; setContentView R layout activity main ; if savedInstanceState null { isFastScroll savedInstanceState ...

    android activity bundle 通信示例

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_target); Intent intent = getIntent(); Bundle bundle = intent.getExtras();...

    Android代码-CatLoadingView

    Android CatLoadingView This project idea is from Link. ...@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    安卓Android源码——Bundle.rar

    4. **保存Activity状态**:当Activity被销毁并重新创建(例如设备旋转导致配置改变)时,可以使用onSaveInstanceState(Bundle outState)方法保存当前状态到Bundle,然后在onCreate(Bundle savedInstanceState)或...

    Android代码-Immersive

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Immersive.setContentView(this, R.layout.activity_main, R.color.blue, R.color.green, false, false); } } ...

    android bundle和activity之间交换数据

    当Activity生命周期中调用`onSaveInstanceState(Bundle outState)`时,开发者可以将关键数据放入outState Bundle,系统会在重建时通过`onCreate(Bundle savedInstanceState)`或`onRestoreInstanceState(Bundle ...

    简易计算器

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { ...

    Android代码-安卓路由解决方案

    HippoRouter Android端URL路由解决方案... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); initDataFromIntent(); } p

    Android代码-RxBus2

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RxBus.get().register(this); } public void test1005(View view){ ...

    Android 柱状图

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); LinearLayout layout = (LinearLayout) findViewById...

    android listview 上下滑动刷新

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pull_to_refresh); ((PullToRefreshListView) getListView()).setOnRefreshListener(new ...

    Android代码-一个简洁的基于 RxJava2 的事件总线库

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 注册 String 类型事件 RxBus.getDefault().subscribe(this, new ...

    Android ListView简单应用

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter&lt;String&gt; arrayAdapter=new ArrayAdapter( MainActivity....

    android时间控件

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initializeViews(); final Calendar c = Calendar.getInstance()...

    Android代码-TouchRegion

    TouchRegion Scene View整体尺寸小,点击区域小,需要点击很多次才能选中; 通过设置View的padding属性增大... ... ... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set

    一个封装WebView的库

    环境搭建新建一个activity继承WebActivity @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setWebChromeClient(new ChromeClient());//设置...

    Android onCreate( )方法详细介绍

    首先,从Google Android Developers官网的解释来看,`onCreate(Bundle savedInstanceState)`的主要任务是初始化活动。在这个阶段,开发者通常会执行以下操作: 1. **设置UI布局**:调用`setContentView(int ...

    生活轨迹Android端

    protected void onCreate Bundle savedInstanceState { super onCreate savedInstanceState ; YLC APPLICATION app YLC APPLICATION this getApplication ; setContentView R layout activity ylc bus line ;...

    Android代码-简单实用的页面多状态布局(content,loading,empty,error)

    LoadingLayout 简单实用的页面多状态布局(content,...protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ... vLoad

    android界面效果全汇聚

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 指定Activity关闭时的动画以及打开下一个Activity时的动画 ...

Global site tag (gtag.js) - Google Analytics