12

Possible Duplicate:
Check whether activity is active

I want to show a progress dialog on the screen if the activity is showing. But when the activity window is not showing, it will not do anything. Please suggest, how do I check whether my activity is showing or not?

Community
  • 1
  • 1
mudit
  • 25,306
  • 32
  • 90
  • 132

2 Answers2

17

Thanks for quick responses.. but i think i figured my answer. I try-catched the part where i was starting the progress dialog.

Sample code --

try {
        pd2.show();
    } catch (WindowManager.BadTokenException e) {
        // TODO: handle exception
    } catch (Exception e) {
        // TODO: handle exception
    }

So if my activity is in on the top, it will show the progress dialog, otherwise it will throw a BadTokenException which is handled using try catch block.

mudit
  • 25,306
  • 32
  • 90
  • 132
0

Read up on the Activity lifecycle. Specific methods get called based on your actvities current state, whether running, whether paused, killed, etc.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119