Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1463885
  • 博文数量: 267
  • 博客积分: 3010
  • 博客等级: 少校
  • 技术积分: 3089
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-05 17:09
个人简介

尊天命,尽人事

文章分类

全部博文(267)

文章存档

2017年(6)

2015年(4)

2014年(27)

2013年(52)

2012年(59)

2011年(120)

分类: C/C++

2012-10-24 19:20:35

 

activity的生命周期中,只要离开了可见阶段,或者说失去了焦点,activity就很可能被进程终止了!,被KILL掉了,,这时候,就需要有种机制,能保存当时的状态,这就是savedInstanceState的作用。

当一个ActivityPAUSE时,被kill之前,它可以调用onSaveInstanceState()来保存当前activity的状态信息(paused状态时,要被KILLED的时候)。用来保存状态信息的Bundle会同时传给两个method,onRestoreInstanceState() and onCreate().

示例代码如下:

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);

    }

}

阅读(1376) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~