Decompose-Router icon indicating copy to clipboard operation
Decompose-Router copied to clipboard

How can we implement it to transfer data to the previous screen

Open beksar1998 opened this issue 11 months ago • 1 comments

can you give examples?

beksar1998 avatar Feb 21 '25 03:02 beksar1998

I can share my solution: https://gist.github.com/Skeptick/7c354c80b9941576e85c3c3367c57f76

Usage

Screen declaration in graph

@Serializable
data object SomeScreen : Route, Returnable<SomeResult>

Push for result

val resultManager = LocalResultManager.current
val someScreenKey = resultManager.rememberResultKey<_, SomeScreen> { result ->
    // handle result
}

router.pushForResult(
    key = someScreenKey,
    configuration = SomeScreen
)

Return result

val resultManager = LocalResultManager.current

router.popWithResult(
    resultManager = resultManager,
    result = SomeResult()
)

Automatically set result when the screen is closed (if result was not set explicitly via popWithResult)

router.setResultOnPop {
    SomeResult()
}

[!NOTE]
Note that in my solution result is not serialized and will be lost when the Android process dies. If you want it to survive process death, you need to make changes to ResultManager and the RouteAwareKey persistence mechanism.

Skeptick avatar Feb 21 '25 08:02 Skeptick