jenzz / RxAppState

[DEPRECATED] A simple Android library that monitors app state changes (background / foreground).
MIT License
195 stars 20 forks source link

It's not working when startMonitoring after onStart() in Activity #21

Open Double-Dude opened 6 years ago

Double-Dude commented 6 years ago

It's working fine when I have the following code before onStart() method gets code, i.e. in onCreate method. But if I have the following code added after onStart() in Activity, then app enters background onAppDidEnterForeground will not get fired at the first time. If I come back to foreground and re-enter background, then it works as expected.

AppStateMonitor appStateMonitor = AppStateMonitor.create(application);
 appStateMonitor.addListener(this);
 appStateMonitor.start();
jenzz commented 6 years ago

Yep. The library internally relies on the onStart() callback so I suggest starting the app state monitor in onCreate() of either your Application or Activity class.

Is there any particular reason you only want to kick it off in onResume()?

Double-Dude commented 6 years ago

Hi @jenzz , I put the appStateMonitor.start() in a xxxClient class. The client is started in Fragment's onCreate/onCreateView method. So I found out the Fragment's onCreate will only get called after Activity's onStart() if you use transcation.commit().

So the appStateMonitor.start() will be called after onStart() of Activity class and it doesn't work well at the first entering background.

jenzz commented 6 years ago

So I found out the Fragment's onCreate will only get called after Activity's onStart() if you use transcation.commit().

This obviously depends on when you are performing the Fragment transaction in your Activity.

I'm happy to have a look at it, if you can provide some sample code.