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