2 ms·
>Toasts use to work for quick notifications, now, after API 31, it only works if the app is foreground. How to know if you app in foreground? You have to use Ac
by charcircuit 1y ago
>Toasts use to work for quick notifications, now, after API 31, it only works if the app is foreground. How to know if you app in foreground? You have to use ActivityLifecycleCallbacks for that and write ton of code and even then there are confusions about onStart vs onResume. Of course this can race with the check in the OS so I question if this is even a good idea in the first place.
The check in the OS just checks if your app currently has a resumed activity. It doesn't require a ton of code. All activities receive a callback when they become resumed, onResume, and a callback when they are paused onPause. Yes you can use ActivityLifecycleCallbacks to have callbacks for all activities of your app in one place to track the state for each activity one place if you are not firing the toast from a specific Activity.
The onStart callback is not relevant here. And the article that was linked improperly assumes that an app only has one Activity going through lifecycle events at once which is not true. The article is trying to solve a different problem, a problem that probably shouldn't need to be solved. It's trying to avoid callbacks when an app gets recreated when the device rotates, but nowhere else in the OS avoids such a thing. During rotations the OS freezes the display to hide the fact that your app is closing and reopening. It's going to be hacky to trying and pretend to stay open during such an event.
- ashishb 1y agoThis comment itself proves what should have be `context.isInFoteground()` function call has been made more complicated.
- charcircuit 1y agoThere isn't a single definition of what foreground means and typically usage of something like that would be a signal of poor design leading you to the point of wanting such a thing.
- ashishb 1y agoToast can only be displayed when the app is the foreground. If there is not single definition of what foreground is then how is someone supposed to know whether a toast will be displayed or not?
- charcircuit 1y agoIf someone wants to know the real definition they need to read the AOSP code. The code is the source of truth and it could potentially change in a future versions if a new security concern came in. https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java;l=10104 https://cs.android.com/android/platform/superproject/main/+/... Whether a toast is successfully toasted is reported back to the app's process, but the android framework doesn't expose this result to the app which implies that one shouldn't need to rely on the toast being shown or not.
- ashishb 1y ago> which implies that one shouldn't need to rely on the toast being shown or not. I wanted to display Toast if I can, and if not, a developer might prefer to display a Snackbar or a notification if a Toast cannot be displayed.