Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Text File Editor losing unsaved changes when rotating screen #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ReadTextActivity : SimpleActivity() {
}

private val binding by viewBinding(ActivityReadTextBinding::inflate)
private val keyUnsavedText = "KEY_UNSAVED_TEXT"

private var filePath = ""
private var originalText = ""
Expand Down Expand Up @@ -85,7 +86,7 @@ class ReadTextActivity : SimpleActivity() {

binding.readTextView.onGlobalLayout {
ensureBackgroundThread {
checkIntent(uri)
checkIntent(uri, savedInstanceState)
}
}

Expand All @@ -97,6 +98,13 @@ class ReadTextActivity : SimpleActivity() {
setupToolbar(binding.readTextToolbar, NavigationIcon.Arrow)
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
if (originalText != binding.readTextView.text.toString()) {
outState.putString(keyUnsavedText, binding.readTextView.text.toString())
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == SELECT_SAVE_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
Expand Down Expand Up @@ -241,7 +249,7 @@ class ReadTextActivity : SimpleActivity() {
}
}

private fun checkIntent(uri: Uri) {
private fun checkIntent(uri: Uri, savedInstanceState: Bundle?) {
originalText = if (uri.scheme == "file") {
filePath = uri.path!!
val file = File(filePath)
Expand Down Expand Up @@ -270,7 +278,13 @@ class ReadTextActivity : SimpleActivity() {
}

runOnUiThread {
binding.readTextView.setText(originalText)
var textToSet = originalText

if (savedInstanceState != null) {
textToSet = savedInstanceState.getString(keyUnsavedText, originalText)
}

binding.readTextView.setText(textToSet)
if (originalText.isNotEmpty()) {
hideKeyboard()
} else {
Expand Down