【Android】Activity 的恢复

Activity 被回收,在 onSaveInstanceState 存入数据,在 onCreate 恢复数据。

override fun onSaveInstanceState(outState: Bundle) { 
	super.onSaveInstanceState(outState)
	val tempData = "Something you just typed" 
	outState.putString("data_key", tempData)
}
override fun onCreate(savedInstanceState: Bundle?) { 
	super.onCreate(savedInstanceState)
	Log.d(tag, "onCreate") 
	setContentView(R.layout.activity_main)
	
	if (savedInstanceState != null) {
		val tempData = savedInstanceState.getString("data_key")
		Log.d(tag, "tempData is $tempData") 
	}
	... 
}

你可能感兴趣的:(Android,android)