kanawish / android-mvi-sample

Example MVI implementation, based off of Google's architectural samples.
303 stars 56 forks source link

Back button navigation doesn't work (add/edit task screen) #8

Closed ziem closed 1 year ago

ziem commented 5 years ago

Hello! When I was doing your course I found this issue. When you try to go back from the AddEditTaskActivity using back button you are constantly redirected to the AddEditTaskActivity. This happens because the task state is never changed to Closed, it's still in the Editing state and when TasksActivity resubscribes it opens AddEditTaskActivity again.

This is how I fixed it:

@@ -0,0 +1,43 @@
diff --git a/app/src/main/java/com/kanawish/sample/mvi/view/addedittask/AddEditTaskActivity.kt b/app/src/main/java/com/kanawish/sample/mvi/view/addedittask/AddEditTaskActivity.kt
index 7fffcbf..4efddb7 100644
--- a/app/src/main/java/com/kanawish/sample/mvi/view/addedittask/AddEditTaskActivity.kt
+++ b/app/src/main/java/com/kanawish/sample/mvi/view/addedittask/AddEditTaskActivity.kt
@@ -43,6 +43,8 @@ class AddEditTaskActivity : AppCompatActivity(),
      */
     private val navigateUpRelay = PublishRelay.create<CancelTaskClick>()

+    private val navigateBackRelay = PublishRelay.create<CancelTaskClick>()
+
     private val disposables = CompositeDisposable()

     override fun Observable<TaskEditorState>.subscribeToState(): Disposable {
@@ -62,7 +64,7 @@ class AddEditTaskActivity : AppCompatActivity(),
                         busy.visibility = VISIBLE
                     }
                     TaskEditorState.Closed -> {
-                        onBackPressed()
+                        finish()
                     }
                 }
             }
@@ -78,7 +80,8 @@ class AddEditTaskActivity : AppCompatActivity(),
                     .filter { it.itemId == R.id.menu_delete }
                     .map { DeleteTaskClick },
                 fab_edit_task_done.clicks().map { SaveTaskClick },
-                navigateUpRelay
+                navigateUpRelay,
+                navigateBackRelay
         )
     }

@@ -140,4 +143,9 @@ class AddEditTaskActivity : AppCompatActivity(),
         super.onPause()
         disposables.clear()
     }
+
+    override fun onBackPressed() {
+        navigateBackRelay.accept(CancelTaskClick)
+        super.onBackPressed()
+    }
 }
\ No newline at end of file
tenSunFree commented 4 years ago

@ziem Thanks for sharing.

kanawish commented 4 years ago

Thanks for the fix share. I'll adjust the sample (probably over the week-end) before closing this issue.

kanawish commented 4 years ago

I just noticed these issues are from 2019 <slaps-wrists/> hopefully that problem was fixed a while back, but I'll still double check it.