1

I have a screen listening for a data class that contains everything I need. ScreenState. Whenever the user press a button I send the event to a ViewModel. This specific event is just getting the intent and setting on the ScreenState parameter like this.

screenStateFlow.emit(
    ScreenState(
        Intent(...)
    )
)

What happens there is, first time works (User leaves the app and then comeback to the app). When user comebacks to app and there's not any data from the intent and want them to be able to start an intent again. So it does the same action.

Triggers a specific event which gets the intent and sets on the ScreenState parameter and this value is emited, again

And here lays the problem. Value is the same. So compose doesn't recompose itself.

And this solution works. You could say that I don't need all of this and it could work by just starting the intent without having to go through the event process and etc.. But I want it that way (unless I don't find a proper solution)

screenStateFlow.emit(
    ScreenState(
        Intent(...),
        !triggerRecompose
    )
)

Is there any better solution?

Edit: Someone having the same issue as me, the provided answer didn't work. I've already tried the MutableState and the State from compose in ViewModel. Didn't work

Barrufet
  • 495
  • 1
  • 11
  • 33
  • "I have a screen listening for a object" -- what does this mean? "Whenever this object repeats itself (For example the same string) compose won't do any action" -- I think that is `StateFlow`, not Compose. `StateFlow` works on content equality, so it does not actually do anything if you attempt to emit a value that is equal to what was last emitted. – CommonsWare Aug 21 '21 at 20:57
  • Yeah sorry meant a data class. Like `StateViewModel`. I tried using StateFlow and SharedFlow. Not working because I'm passing the same values to this data class as the previous one. – Barrufet Aug 21 '21 at 20:59
  • This is not the correct approach. If you want the state to be triggered, there must be some reason for that. Like if you want some UI component refreshed or something. Hence, you should modify that very variable instead of manually triggering a recomposition. Post some code for the use case if you're not sure how. – Richard Onslow Roper Aug 21 '21 at 21:03
  • Alright, give me one second to edit the post which my specific situation. @MARSK – Barrufet Aug 21 '21 at 21:04
  • Already edited @MARSK – Barrufet Aug 21 '21 at 21:16

1 Answers1

1

I had a similar issue in which I wanted to trigger a snackbar even if the value is repeated.

I solved it by adding a variable parameter to my message object (such as timestamp or Math.random()).

In this way, even if the message content is the same, the Message object is different and it triggers a state change.

nawrasg
  • 138
  • 3
  • 9
  • Yeah I did tje same with a booean trigger by changing the previous value like this `!previousValue` in the parameter, but I still don't like this solution – Barrufet Nov 10 '21 at 14:30
  • I have the same problem too and I reached this solution but I think it is not a proper solution. Please lemme know if you find a better solution. Thanks – Yousef Kazemi Feb 16 '23 at 16:44