Skip to content

Commit

Permalink
Merge pull request #164 from VitalyPeryatin/bug/action_after_on_save_…
Browse files Browse the repository at this point in the history
…instance_state

Fix bug with 'Can not perform this action after onSaveInstanceState'
  • Loading branch information
terrakok authored Jul 20, 2021
2 parents cbaabaf + 8521b7e commit 2a4969d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.github.terrakok.cicerone

import android.os.Handler
import android.os.Looper

/**
* Passes navigation command to an active [Navigator]
* or stores it in the pending commands queue to pass it later.
*/
internal class CommandBuffer : NavigatorHolder {
private var navigator: Navigator? = null
private val pendingCommands = mutableListOf<Array<out Command>>()
private val mainHandler = Handler(Looper.getMainLooper())

override fun setNavigator(navigator: Navigator) {
this.navigator = navigator
Expand All @@ -24,6 +28,8 @@ internal class CommandBuffer : NavigatorHolder {
* @param commands navigation command array
*/
fun executeCommands(commands: Array<out Command>) {
navigator?.applyCommands(commands) ?: pendingCommands.add(commands)
mainHandler.post {
navigator?.applyCommands(commands) ?: pendingCommands.add(commands)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.github.terrakok.cicerone.androidx

import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Handler
import android.os.Looper
import androidx.fragment.app.*
import com.github.terrakok.cicerone.*

Expand All @@ -22,15 +20,8 @@ open class AppNavigator @JvmOverloads constructor(
) : Navigator {

protected val localStackCopy = mutableListOf<String>()
private val mainHandler = Handler(Looper.getMainLooper())

override fun applyCommands(commands: Array<out Command>) {
mainHandler.post {
applyCommandsSync(commands)
}
}

protected open fun applyCommandsSync(commands: Array<out Command>) {
fragmentManager.executePendingTransactions()

//copy stack before apply commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class MainActivity : MvpAppCompatActivity(), ChainHolder {

private val navigator: Navigator = object : AppNavigator(this, R.id.main_container) {

override fun applyCommandsSync(commands: Array<out Command>) {
super.applyCommandsSync(commands)
override fun applyCommands(commands: Array<out Command>) {
super.applyCommands(commands)
supportFragmentManager.executePendingTransactions()
printScreensScheme()
}
Expand Down

0 comments on commit 2a4969d

Please sign in to comment.