android onSaveInstanceState的使用方法
2012-03-14 17:12
531 查看
package com.saveInstanceDemo.src; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class saveInstanceDemo extends Activity { private static final String TAG = "MyNewLog"; private int mCount = 0; private boolean mThreadDisble; @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); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while(true) { if(!mThreadDisble) { Log.v(TAG, Boolean.toString(mThreadDisble)); try{ Thread.sleep(1000); } catch(Exception e) { e.printStackTrace(); } mCount++; Log.v(TAG, "mCount : " + mCount); } } } }).start(); 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", mCount); savedInstanceState.putString("StrTest", "savedInstanceState test"); super.onSaveInstanceState(savedInstanceState); Log.e(TAG, "onSaveInstanceState"); } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); int mCount = savedInstanceState.getInt("IntTest"); String StrTest = savedInstanceState.getString("StrTest"); Log.e(TAG, "onRestoreInstanceState+IntTest="+mCount+"+StrTest="+StrTest); } public void onDestroy() { super.onDestroy(); this.mThreadDisble = true; } public void onPause() { Log.v(TAG, "onPause"); super.onPause(); mThreadDisble = true; } public void onResume() { Log.v(TAG, "onResume"); super.onResume(); mThreadDisble = false; } }
相关文章推荐
- Android BaseSavedState 以及 Activity onSaveInstanceState 和 onRestoreInstanceState 方法使用注意事项
- android onSaveInstanceState的使用方法
- android onSaveInstanceState的使用方法
- Android 中 onSaveInstanceState()使用方法详解
- android onSaveInstanceState的使用方法
- Android使用详解之onSaveInstanceState和onRestoreInstanceState触发的时机
- Android使用详解之onSaveInstanceState和onRestoreInstanceState触发的时机
- onSaveInstanceState()方法使用注意点
- android onSaveInstanceState方法
- Android的 onSaveInstanceState()方法之我浅见
- Android使用详解之onSaveInstanceState和onRestoreInstanceState触发的时机
- android Activity细节(一) onSaveInstanceState()方法使用注意点
- [转]android onSaveInstanceState方法
- android onSaveInstanceState方法 横坚屏切换
- Android 状态保存 生命周期 onSaveInstanceState onRestoreInstanceState 方法
- android onSaveInstanceState方法
- Android中用onSaveInstanceState保存Fragment状态的方法
- Android onSaveInstanceState的使用
- Android编程——自定义View类onSaveInstanceState与onRestoreInstanceState使用方法
- Android开发之Activity的onSaveInstanceState()和 onRestoreInstanceState()方法