android 使用InstanceState儲存和恢復資料
阿新 • • 發佈:2019-02-15
旋轉螢幕前logpackage 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)); } }