package com.example.yk.onsaveinstancestatetest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
/**
* Android使用InstanceState保存和恢复数据
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView textView,textView2;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.text);
textView2= (TextView) findViewById(R.id.text2);
textView2.setOnClickListener(this);
Log.e(TAG,"onCreate");
if(savedInstanceState!=null){
Log.e(TAG,"onCreate恢复的数据:"+savedInstanceState.get("save_data"));
}else {
Log.e(TAG,"onCreate中的bundle为null");
}
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG,"onStar");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e(TAG,"onRestart");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG,"onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG,"onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG,"onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG,"onDestroy");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.e(TAG,"onSaveInstanceState");
outState.putString("save_data","ka ka");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.e(TAG,"onRestoreInstanceState");
Log.e(TAG,"onRestoreInstanceState恢复的数据:"+savedInstanceState.getString("save_data"));
}
@Override
public void onClick(View view) {
// InputMethodManager manager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// manager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
// Log.e(TAG,"弹出键盘");
startActivity(new Intent(this,Main3Activity.class));
}
}
旋转屏幕前log
参考文章:
onSaveInstanceState和onRestoreInstanceState触发的时机 :http://ice-k.iteye.com/blog/1018956
Android入门- 关于Activity下的onCreate方法:http://www.cnblogs.com/shaocm/archive/2012/11/30/2796105.html
Android Notes - Activity生命周期中的onSaveInstanceState:http://hukai.me/android-activitylifecycle-onsaveinstancestate/
Android开发之InstanceState详解:http://www.cnblogs.com/hanyonglu/archive/2012/03/28/2420515.html