[译]Android Activity 和 Fragment 状态保存与恢复的最佳实践
The Real Best Practices to Save/Restore Activity's and Fragment's state. (StatedFragment is now deprecated)
代码记录
package com.t.hencustomeviews
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.core.widget.addTextChangedListener
import com.t.hencustomeviews.databinding.ActDataSaveBinding
class ActDataSave : AppCompatActivity() {
private lateinit var binding: ActDataSaveBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActDataSaveBinding.inflate(layoutInflater)
setContentView(binding.root)
do2()
}
//https://www.jianshu.com/p/45cc7775a44b
//region 1.测试View状态保存与恢复 do1
/**
* TODO 测试View状态保存与恢复
* binding.tv android:freezeText 默认为false
* binding.et android:freezeText 默认为true
* 测试:屏幕旋转
* 屏幕旋转会导致界面重绘,先调用onDestroy再调用onCreate
* 点击tv区域加载文字然后旋转测试
*/
private fun do1() {
Log.e("ActDataSave", "onCreate")
binding.tv.setOnClickListener {
binding.tv.text = "sth"
binding.et.setText("sssss")
}
}
//endregion do1
//region 2.测试Activity状态保存与恢复 do2
/**
* TODO 2.1测试Activity状态保存与恢复
* binding.tv android:freezeText 默认为false,设置为true
* binding.et android:freezeText 默认为true
* binding.tv1 android:freezeText 默认为false
* 测试:屏幕旋转
* 屏幕旋转会导致界面重绘,先调用onDestroy再调用onCreate
* 点击tv区域加载文字然后旋转测试
* 结果:text1内容没有保存,binding.tv1旋转后为空
*/
private var text1 = ""
private fun do2() {
binding.tv.setOnClickListener {
text1 = "click binding textView"
binding.tv1.text = text1
binding.tv.text = text1
binding.et.setText(text1)
}
}
//View 的状态可以被自动保存,但是 Activity 成员变量却不行。他们将随着 Activity 一起被销毁。我们需要手动保存和恢复
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putSerializable("text1", text1)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
text1 = savedInstanceState.getString("text1") ?: ""
}
override fun onResume() {
super.onResume()
//should init in onResume ,after onRestoreInstanceState call
binding.tv1.text = text1
}
//endregion
//Fragment状态保存与恢复
//一旦 Fragment 从回退栈(BackStack)中返回时,View 将会被销毁和重建。
// 这种情况属于,Fragment 没有被销毁,但 Fragment 的 View 被销毁。
// View状态保存与恢复遵循do1()
// 而在这种情况下只有 View 被销毁和重建。Fragment 实例仍然在那儿,包括实例里的成员变量。所以不需要对成员变量做任何事情。
}
act_data_save.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ActDataSave">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#130f30"
android:gravity="center"
android:textColor="@color/white"
android:freezesText="true"
android:textSize="18sp"
tools:text="TextView" />
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="100dp"
android:inputType="text" />
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#130f30"
android:gravity="center"
android:textColor="@color/white"
android:textSize="18sp"
tools:text="TextView" />
</LinearLayout>
网友评论