I've just started learning Android. I use my MIUI 10 device for debugging.
I've learned the very basic App Lifecycle Program. The problem is, I'm using toasts to show when each of the methods are being called. For eg: When I press the back button, onPause, onStop and onDestroy are 3 SEPERATE toasts that will be displayed.
The problem I'm facing is that MIUI will automatically cancel the current toast if another one is called. So I end up with only onDestroy toast displaying.
Is there a way to ensure I have the toast on screen for a set amount of time before the next one comes? This doesn't have to only apply to this scenario. I need a general solution that will help in the future as well.
If MIUI's version of Android has altered the default behaviour where Toasts are displayed sequentially, you can always post them with a delay yourself. The standard delay period for a long toast is 3500ms, and a short toast is 2000ms. With that in mind, you could try something along these lines (untested):
final Handler uiHandler = new Handler(Looper.getMainLooper());
void scheduleToasts(String... messages) {
final List<String> list = Arrays.asList(messages);
Collections.reverse(list);
final AtomicInteger countdown = new AtomicInteger(list.size());
final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleWithFixedDelay(() -> {
final int count = countdown.getAndDecrement();
if(count == 0) {
service.shutdown();
return;
}
uiHandler.post(() -> Toast.makeText(getContext(), list.get(count), Toast.LENGTH_LONG).show());
}, 0, 3500, TimeUnit.MILLISECONDS);
}
With usage:
scheduleToasts("message1", "message2", "message3");