android onSaveInstanceState的使用方法

[java]  view plain  copy
  1.    
  2. package com.saveInstanceDemo.src;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. public class saveInstanceDemo extends Activity {  
  8.     
  9.     
  10.     private static final String TAG = "MyNewLog";  
  11.     private int mCount = 0;  
  12.     private boolean mThreadDisble;  
  13.     
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         // If an instance of this activity had previously stopped, we can  
  18.         // get the original text it started with.  
  19.         if(null != savedInstanceState)  
  20.         {  
  21.             int IntTest = savedInstanceState.getInt("IntTest");  
  22.             String StrTest = savedInstanceState.getString("StrTest");  
  23.             Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);         
  24.         }  
  25.         setContentView(R.layout.main);  
  26.         
  27.         new Thread(new Runnable()  
  28.   {  
  29.    @Override  
  30.    public void run() {  
  31.     // TODO Auto-generated method stub  
  32.      
  33.     while(true)  
  34.     {  
  35.      if(!mThreadDisble)  
  36.      {  
  37.       Log.v(TAG, Boolean.toString(mThreadDisble));  
  38.       try{  
  39.        Thread.sleep(1000);  
  40.       }  
  41.       catch(Exception e)  
  42.       {  
  43.        e.printStackTrace();  
  44.       }  
  45.       mCount++;  
  46.       Log.v(TAG, "mCount : " + mCount);  
  47.      }  
  48.     }      
  49.    }  
  50.   }).start();  
  51.    
  52.         Log.e(TAG, "onCreate");  
  53.     }  
  54.     @Override  
  55.     //为了防止万一程序被销毁的风险,这个方法可以保证重要数据的正确性  
  56.     //不写这个方法并不意味着一定出错,但是一旦遇到了一些非常奇怪的数据问题的时候  
  57.     //可以看看是不是由于某些重要的数据没有保存,在程序被销毁时被重置  
  58.     public void onSaveInstanceState(Bundle savedInstanceState) {  
  59.         // Save away the original text, so we still have it if the activity  
  60.         // needs to be killed while paused.  
  61.       savedInstanceState.putInt("IntTest", mCount);  
  62.       savedInstanceState.putString("StrTest""savedInstanceState test");  
  63.       super.onSaveInstanceState(savedInstanceState);  
  64.       Log.e(TAG, "onSaveInstanceState");  
  65.     }    
  66.     @Override  
  67.     public void onRestoreInstanceState(Bundle savedInstanceState) {  
  68.       super.onRestoreInstanceState(savedInstanceState);  
  69.       int mCount = savedInstanceState.getInt("IntTest");  
  70.       String StrTest = savedInstanceState.getString("StrTest");  
  71.       Log.e(TAG, "onRestoreInstanceState+IntTest="+mCount+"+StrTest="+StrTest);  
  72.     }  
  73.     
  74.  public void onDestroy()  
  75.  {  
  76.   super.onDestroy();  
  77.   this.mThreadDisble = true;  
  78.  }  
  79.    
  80.  public void onPause()  
  81.  {  
  82.   Log.v(TAG, "onPause");  
  83.   super.onPause();  
  84.   mThreadDisble = true;  
  85.  }  
  86.    
  87.  public void onResume()  
  88.  {  
  89.   Log.v(TAG, "onResume");  
  90.   super.onResume();  
  91.   mThreadDisble = false;  
  92.  }  
  93. }  

[java]  view plain  copy
  1.    
  2. package com.saveInstanceDemo.src;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. public class saveInstanceDemo extends Activity {  
  8.     
  9.     
  10.     private static final String TAG = "MyNewLog";  
  11.     private int mCount = 0;  
  12.     private boolean mThreadDisble;  
  13.     
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         // If an instance of this activity had previously stopped, we can  
  18.         // get the original text it started with.  
  19.         if(null != savedInstanceState)  
  20.         {  
  21.             int IntTest = savedInstanceState.getInt("IntTest");  
  22.             String StrTest = savedInstanceState.getString("StrTest");  
  23.             Log.e(TAG, "onCreate get the savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);         
  24.         }  
  25.         setContentView(R.layout.main);  
  26.         
  27.         new Thread(new Runnable()  
  28.   {  
  29.    @Override  
  30.    public void run() {  
  31.     // TODO Auto-generated method stub  
  32.      
  33.     while(true)  
  34.     {  
  35.      if(!mThreadDisble)  
  36.      {  
  37.       Log.v(TAG, Boolean.toString(mThreadDisble));  
  38.       try{  
  39.        Thread.sleep(1000);  
  40.       }  
  41.       catch(Exception e)  
  42.       {  
  43.        e.printStackTrace();  
  44.       }  
  45.       mCount++;  
  46.       Log.v(TAG, "mCount : " + mCount);  
  47.      }  
  48.     }      
  49.    }  
  50.   }).start();  
  51.    
  52.         Log.e(TAG, "onCreate");  
  53.     }  
  54.     @Override  
  55.     //为了防止万一程序被销毁的风险,这个方法可以保证重要数据的正确性  
  56.     //不写这个方法并不意味着一定出错,但是一旦遇到了一些非常奇怪的数据问题的时候  
  57.     //可以看看是不是由于某些重要的数据没有保存,在程序被销毁时被重置  
  58.     public void onSaveInstanceState(Bundle savedInstanceState) {  
  59.         // Save away the original text, so we still have it if the activity  
  60.         // needs to be killed while paused.  
  61.       savedInstanceState.putInt("IntTest", mCount);  
  62.       savedInstanceState.putString("StrTest""savedInstanceState test");  
  63.       super.onSaveInstanceState(savedInstanceState);  
  64.       Log.e(TAG, "onSaveInstanceState");  
  65.     }    
  66.     @Override  
  67.     public void onRestoreInstanceState(Bundle savedInstanceState) {  
  68.       super.onRestoreInstanceState(savedInstanceState);  
  69.       int mCount = savedInstanceState.getInt("IntTest");  
  70.       String StrTest = savedInstanceState.getString("StrTest");  
  71.       Log.e(TAG, "onRestoreInstanceState+IntTest="+mCount+"+StrTest="+StrTest);  
  72.     }  
  73.     
  74.  public void onDestroy()  
  75.  {  
  76.   super.onDestroy();  
  77.   this.mThreadDisble = true;  
  78.  }  
  79.    
  80.  public void onPause()  
  81.  {  
  82.   Log.v(TAG, "onPause");  
  83.   super.onPause();  
  84.   mThreadDisble = true;  
  85.  }  
  86.    
  87.  public void onResume()  
  88.  {  
  89.   Log.v(TAG, "onResume");  
  90.   super.onResume();  
  91.   mThreadDisble = false;  
  92.  }  
  93. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值