views:

50

answers:

1

I used Toast to make notification, but it seems it will appear even its activity is not in the current screen and some other activity has been started.

I want to check this situation, when the activity is not the current one, I'd not send the Toast notification. But how to do ?

A: 

When your Activity comes to the foreground, its onResume() method will be invoked. When another Activity comes in front of your Activity, its onPause() method will be invoked. So all you need to do is implement a boolean indicating if your Activity is in the foreground:

private boolean isInFront;

public void onResume() {
    isInFront = true;
}

public void onPause() {
    isInFront = false;
}
Andy Zhang
Any convenient method exist? like Activety::isActive()
virsir
I haven't personally come across or used such a method, but the above solution is very convenient too, in my opinion.
Andy Zhang