Skip to content

Commit 97dcf30

Browse files
fix: save state when rotating screen (#111)
* Fixed Text File Editor losing unsaved changes when rotating screen * style: replace state variable with constant --------- Co-authored-by: Naveen Singh <[email protected]> Co-authored-by: Naveen Singh <[email protected]>
1 parent 5dae7f8 commit 97dcf30

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

app/src/main/kotlin/org/fossify/filemanager/activities/ReadTextActivity.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ReadTextActivity : SimpleActivity() {
3232
companion object {
3333
private const val SELECT_SAVE_FILE_INTENT = 1
3434
private const val SELECT_SAVE_FILE_AND_EXIT_INTENT = 2
35+
private const val KEY_UNSAVED_TEXT = "KEY_UNSAVED_TEXT"
3536
}
3637

3738
private val binding by viewBinding(ActivityReadTextBinding::inflate)
@@ -85,7 +86,7 @@ class ReadTextActivity : SimpleActivity() {
8586

8687
binding.readTextView.onGlobalLayout {
8788
ensureBackgroundThread {
88-
checkIntent(uri)
89+
checkIntent(uri, savedInstanceState)
8990
}
9091
}
9192

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

101+
override fun onSaveInstanceState(outState: Bundle) {
102+
super.onSaveInstanceState(outState)
103+
if (originalText != binding.readTextView.text.toString()) {
104+
outState.putString(KEY_UNSAVED_TEXT, binding.readTextView.text.toString())
105+
}
106+
}
107+
100108
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
101109
super.onActivityResult(requestCode, resultCode, resultData)
102110
if (requestCode == SELECT_SAVE_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
@@ -241,7 +249,7 @@ class ReadTextActivity : SimpleActivity() {
241249
}
242250
}
243251

244-
private fun checkIntent(uri: Uri) {
252+
private fun checkIntent(uri: Uri, savedInstanceState: Bundle?) {
245253
originalText = if (uri.scheme == "file") {
246254
filePath = uri.path!!
247255
val file = File(filePath)
@@ -270,7 +278,13 @@ class ReadTextActivity : SimpleActivity() {
270278
}
271279

272280
runOnUiThread {
273-
binding.readTextView.setText(originalText)
281+
var textToSet = originalText
282+
283+
if (savedInstanceState != null) {
284+
textToSet = savedInstanceState.getString(KEY_UNSAVED_TEXT, originalText)
285+
}
286+
287+
binding.readTextView.setText(textToSet)
274288
if (originalText.isNotEmpty()) {
275289
hideKeyboard()
276290
} else {

0 commit comments

Comments
 (0)