이 포스팅은 아래 구글 코드랩을 개인 학습용으로 정리한 글입니다.
Each stage in an activity's lifecycle has a corresponding callback method:
onCreate(), onStart(), onPause(), ...
When an activity changes state, the associated callback method is invoked.
The activity state can also change in response to device-configuration changes
-> ex. when the user rotates the device from portrait to landscape
when configuration changes happen,
-> the activity is destroyed and recreated in its default state
-> the user might lose information that they've entered in the activity.
The state of each Activity is stored as a set of key/value pairs
-> in a Bundle object called the Activity instance state
The system saves default state information to instance state Bundle just before the Activity is stopped
-> passes that Bundle to the new Activity instance to restore
the onSaveInstanceState() method
The system calls this method on your Activity
-> between onPause() and onStop()
-> when there is a possibility the Activity may be destroyed and recreated.
The data you save in the instance state is specific to only this instance of this specific Activity during the current app session
When you stop and restart a new app session
-> the Activity instance state is lost
-> the Activity reverts to its default appearance
If you need to save user data between app sessions
-> use shared preferences or a database
➕ 화면 회전으로 구성 변경되면 main activity는 destroyed and recreated
-> main activity 상태 디폴트로 돌아감
-> reply header, reply text 가시성 디폴트인 invisible로 돌아감
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
➕ In Kotlin:
override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) }
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
if (mReplyHeadTextView.visibility == View.VISIBLE) {
outState.putBoolean("reply_visible", true)
outState.putString("reply_text",mReplyTextView.text.toString());
}
}
Once you've saved the Activity instance state
-> you also need to restore it when the Activity is recreated
You can do this either in onCreate()
or by implementing the onRestoreInstanceState() callback(called after onStart() after the Activity is created)
Most of the time the better place to restore the Activity state is in onCreate()
-> ensure that the UI(including the state) is available as soon as possible.
It is sometimes convenient to do it in onRestoreInstanceState()
-> after all of the initialization has been done
-> allow subclasses to decide whether to use your default implementation.
// Restore the state.
if (savedInstanceState != null) {
}
When your Activity is created
-> the system passes the state Bundle to onCreate() as its only argument
The first time onCreate() is called
-> the Bundle is null
-> there's no existing state the first time your app starts.
Subsequent calls to onCreate()
-> have a bundle populated with the data you stored in onSaveInstanceState().
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d(TAG, "-------");
Log.d(TAG, "onCreate");
mMessageEditText = findViewById(R.id.editText_main)
mReplyHeadTextView = findViewById(R.id.text_header_reply)
mReplyTextView = findViewById(R.id.text_message_reply)
if(savedInstanceState != null){
val isVisible : Boolean = savedInstanceState.getBoolean("reply_visible")
if(isVisible){
mReplyHeadTextView.visibility = View.VISIBLE
mReplyTextView.visibility = View.VISIBLE
mReplyTextView.text = savedInstanceState.getString("reply_text")
}
}
}