android基础学习

来源:互联网 发布:类似于faceu的软件 编辑:程序博客网 时间:2024/05/17 07:13

super.onCreate(savedInstanceState)是调用父类的onCreate构造函数

savedInstanceState是保存当前Activity的状态信息

    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参数,就很容易解决上述问题。


Bundle类是一个key-value对,“A mapping from String values to various Parcelable types.

根据google官方的文档(http://developer.android.com/reference/android/os/Bundle.html)

类继承关系:

java.lang.Object
     android.os.Bundle

Bundle类是一个final类:
public final class
Bundle
extends Objectimplements Parcelable Cloneable

两个activity之间的通讯可以通过bundle类来实现,做法就是:

(1)新建一个bundle类

[java] view plaincopy
  1. Bundle mBundle = new Bundle();   
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)

[java] view plaincopy
  1. mBundle.putString("Data""data from TestBundle");  

(3)新建一个intent对象,并将该bundle加入这个intent对象

[cpp] view plaincopy
  1. Intent intent = new Intent();    
  2. intent.setClass(TestBundle.this, Target.class);    
  3. intent.putExtras(mBundle);  
代码如下:

两个类如下:intent从TestBundle类发起,到Target类。

类1:TestBundle类:

[java] view plaincopy
  1. import android.app.Activity;    
  2. import android.content.Intent;    
  3. import android.os.Bundle;    
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7.   
  8. public class TestBundle extends Activity {    
  9.       
  10.     private Button button1;  
  11.     private OnClickListener cl;   
  12.     public void onCreate(Bundle savedInstanceState) {    
  13.         super.onCreate(savedInstanceState);    
  14.         setContentView(R.layout.main);  
  15.           
  16.         button1 = (Button) findViewById(R.id.button1);  
  17.         cl = new OnClickListener(){  
  18.             @Override  
  19.             public void onClick(View arg0) {  
  20.                 // TODO Auto-generated method stub  
  21.                 Intent intent = new Intent();    
  22.                 intent.setClass(TestBundle.this, Target.class);    
  23.                 Bundle mBundle = new Bundle();    
  24.                 mBundle.putString("Data""data from TestBundle");//压入数据    
  25.                 intent.putExtras(mBundle);    
  26.                 startActivity(intent);  
  27.             }  
  28.         };  
  29.         button1.setOnClickListener(cl);  
  30.     }  

类2: Target

[java] view plaincopy
  1. import android.app.Activity;    
  2. import android.os.Bundle;    
  3.   
  4. public class Target extends Activity{    
  5.   
  6.     public void onCreate(Bundle savedInstanceState) {    
  7.           
  8.         super.onCreate(savedInstanceState);    
  9.         setContentView(R.layout.target);    
  10.         Bundle bundle = getIntent().getExtras();   </span> //得到传过来的bundle  
  11.         String data = bundle.getString("Data");//读出数据    
  12.         setTitle(data);    
  13.   
  14.     }    
  15. }    

0 0
原创粉丝点击