0

Call API when app state become foreground from background in MVVM structure in android platform.

Andyson
  • 55
  • 1
  • 7
  • use `onStart` or `onResume` (depending on the usecase) to trigger a method on your ViewModel. https://developer.android.com/guide/components/activities/activity-lifecycle – Blundell Jan 11 '23 at 13:16
  • @Blundell I have 5 different screen and I want to trigger same API for every screen when app comes to foreground after background. – Andyson Jan 11 '23 at 14:02
  • If you have 1 activity, then you have 1 callback – Blundell Jan 11 '23 at 14:57

1 Answers1

0

You can use registerActivityLifecycleCallbacks to follow up the lifecycle of your app.

For example usage: https://stackoverflow.com/a/14470360/5686866

enesigneci
  • 301
  • 1
  • 4
  • 13
  • How to get access of repository or viewmodel inside lifecycle class. I want to implement using MVVM structure. – Andyson Jan 11 '23 at 14:00
  • I don't know your project structure but maybe you can use dependency injection to pass your viewmodel to lifecycle class then use it – enesigneci Jan 11 '23 at 14:04