Nastro
Nastro

Reputation: 1769

How to discover what Activity is on the foreground in Android app?

I need to implement the logic - If user on chat activity then I don't need to show push notification with new message. So I need to know what activity is on the screen. For this purpose I found this answer How to get current foreground activity context in android? But I don't understand how to use

public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)

Can some one give me a full example how to discover what activity is on screen?

Upvotes: 1

Views: 2224

Answers (3)

Rajasekaran M
Rajasekaran M

Reputation: 2538

You have to implement onActivityPaused and onActivityResumed()

    public class YourApplication extends Application implements
    Application.ActivityLifecycleCallbacks {

    public static boolean isChatVisible=false;   

    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
    }

    @Override
    public void onActivityCreated(Activity p0, Bundle p1) {

    }

    @Override
    public void onActivityStarted(Activity p0) {

    }

    @Override
    public void onActivityResumed(Activity p0) {

         isChatVisible=p0 instanceof ChatActivity;
    }

    @Override
    public void onActivityPaused(Activity p0) {

    }

    @Override
    public void onActivityStopped(Activity p0) {

    }

}

Before building the notification just check YourApplication.isChatVisible()

Upvotes: 3

Ped7g
Ped7g

Reputation: 16606

Your push notifications are received by service class in your app (and I guess you are interested in foreground activity from your app, i.e. the same code base).

Maybe there are more elegant and smarter solutions, but you can also do a bit crude one like this:

Add flag (static boolean doNotCreateNotification = false; is enough for the simple scenario of chat activity prohibiting notifications) to MyApplication extends Application class (add new one, if you don't have yet one in your project, make sure you reconfigure the project correctly to use custom Application class).

Now this static flag will be pretty much "global" for all the code which is running from your app, including all activities and/or background service processing notifications.

The Activity life cycle guarantees that onResume is called when activity becomes foreground+active one, and onPause when the activity is leaving such state. So you can update the global flag in these to true/false in the onResume/onPause methods.

Then in the service processing received notification you can check if the flag is set to true and just discard the notification data without creating badge in the OS.

Upvotes: 0

user1506104
user1506104

Reputation: 7106

Here is the basic structure of what you need to do:

public class AmpApplication extends Application implements
    Application.ActivityLifecycleCallbacks {

    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        // do nothing
    }

    @Override
    public void onActivityStarted(Activity activity) {
        // do nothing
    }

    @Override
    public void onActivityResumed(Activity activity) {
        if(activity instanceof ChatActivity) {
            // chat activity is in foreground
        }
    }

    @Override
    public void onActivityPaused(Activity activity) {
        // do nothing
    }

    @Override
    public void onActivityStopped(Activity activity) {
        // do nothing
    }

}

Upvotes: 1

Related Questions