简体   繁体   中英

How do I know whether or not my Android app is running in background?

There is a WebView in my Android App.

I make a toast to show the loading progress.

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            super.onProgressChanged(view, progress);

            toast.setText("Loading..." + String.valueOf(progress) + "%");
            toast.show();
        }
    });

The problem is that when I press "home" button or start another Activity, it still keep updating and showing the toast.

Is there way to check if the current Activity is showing?

For instance:

if(getActivity().isVisible())
   toast.show();

You could set yourself a boolean flag in onStop() which gets called as you are being removed from the screen.

private boolean amShowing = false;

protected void onStart(){
    super.onStart();
    amShowing = true;
}

protected void onStop() {
    super.onStop();
    amShowing = false;
}

when with your if statment you can use

if(amShowing) {
   toast.show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM