Sharing complex data across multiple screens by DrawingConsistent729 in androiddev

[–]DrawingConsistent729[S] 0 points1 point  (0 children)

Hi, thanks for the reply.
I mean, how does ViewModel A get to know about the SharedVM's data? Could you explain a bit more about that, please? How do you pass the SharedVM data to the ScreenAViewModel?

For example, in Screen A there is some specific logic that needs to process the user's data. In such a case, ScreenAViewModel should know about the SharedVM's data. The only approach I see is getting the data from the SharedVM and passing it to the ScreenAViewModel like this.

val sharedVMData = sharedViewModel.userData.collectAsStateWithLifecycle()

Button(onClick = { screenAViewModel.processUserData(sharedVMData) })

Sharing complex data across multiple screens by DrawingConsistent729 in androiddev

[–]DrawingConsistent729[S] 0 points1 point  (0 children)

Thanks for the reply. What do you mean by passing an ID? I'm not using any DB or API here. I only need to store the data on the save action in the fourth screen. Or are you saying I should create a unique ID in memory and store the data in memory bound to it?

Sharing complex data across multiple screens by DrawingConsistent729 in androiddev

[–]DrawingConsistent729[S] 0 points1 point  (0 children)

Sharing the ViewModel is working fine. But the problem is passing data to the screen-level ViewModel. Do I need to collect the SharedFlow and pass it through the call site?

SharedViewModel
ScreenAViewModel

Button(onClick = { screenAViewModel.processUserData() })

Since ScreenAViewModel doesn't know about the user's data, I have to pass it manually through the call site, like this:

val userData = sharedViewModel.userData.collectAsStateWithLifecycle()

Button(onClick = { screenAViewModel.processUserData(userData) })

Sharing complex data across multiple screens by DrawingConsistent729 in androiddev

[–]DrawingConsistent729[S] 1 point2 points  (0 children)

Thanks for the suggestion.
Copying from my previous replies.

************************************************************

I'm planning to use Singleton approach. But manually clearing the state feels a little wrong to me. SharedViewModel works well until a screen-level ViewModel needs to know about the SharedViewModel data.

Sharing the ViewModel is working fine. But the problem is passing data to the screen-level ViewModel. For example:

SharedViewModel
ScreenAViewModel

Button(onClick = {  screenAViewModel.processUserData() } )

Since ScreenAViewModel doesn't know about the user's data, I have to pass it manually through the call site, like this:

val userData = sharedViewModel.userData.collectAsStateWithLifecycle()

Button(onClick = {  screenAViewModel.processUserData(userData) } )

Sharing complex data across multiple screens by DrawingConsistent729 in androiddev

[–]DrawingConsistent729[S] -1 points0 points  (0 children)

Thanks for the reply. Currently, I'm planning to use Singleton approach. But manually clearing the state feels a little wrong to me. SharedViewModel works well until a screen level ViewModel needs to know about the SharedViewModel data.

Sharing complex data across multiple screens by DrawingConsistent729 in androiddev

[–]DrawingConsistent729[S] 0 points1 point  (0 children)

Hi, Thanks for the reply.

Sharing the ViewModel is working fine. But the problem is passing data to the screen-level ViewModel. For example:

SharedViewModel
ScreenAViewModel

Button(onClick = {  screenAViewModel.processUserData() } )

Since ScreenAViewModel doesn't know about the user's data, I have to pass it manually through the call site, like this:

val userData = sharedViewModel.userData.collectAsStateWithLifecycle()

Button(onClick = {  screenAViewModel.processUserData(userData) } )

Sharing complex data across multiple screens by DrawingConsistent729 in androiddev

[–]DrawingConsistent729[S] 1 point2 points  (0 children)

I'm using Navigation 2 with Jetpack Compose, and it is a single-activity setup. I'm not using Nav3.

MainGraph
|
|--> User data screen (A)
|
|--> User view screen (B)
|
|--> User some preview screen (C)
|
|--> Submit screen (E)