How can Toasts be made to appear only if the app is in the foreground?

后端 未结 3 761
庸人自扰
庸人自扰 2021-02-09 07:17

I have a Toast that is called from inside a Service. It always appears regardless of the state of the app, as designed.

How can make it appear

3条回答
  •  失恋的感觉
    2021-02-09 07:51

    Just place some flag

    public class MyActivity {
    
        private boolean isInForeground;
    
        protected onResume() {
            isInForeground = true;
        }
    
        protected onPause() {
            isInForeground = false;
        }
    
        private displayToast() {
            if(isInForeground) {
               Toast.makeToast().show();
            }
        }
    
    }
    

提交回复
热议问题