1

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?

Nastro
  • 1,719
  • 7
  • 21
  • 40

3 Answers3

3

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()

Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33
Rajasekaran M
  • 2,478
  • 2
  • 20
  • 30
1

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
    }

}
user1506104
  • 6,554
  • 4
  • 71
  • 89
0

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.

Ped7g
  • 16,236
  • 3
  • 26
  • 63
  • 1
    "Rajasekaran M" answer is in principle doing the same thing, but feels a bit less hack-ish, as you are accessing the `Application` instance through life-cycle callback interface, which is designed for this kind of stuff, while direct access of static fields in custom `Application` is a bit ugly thing (for example makes Unit testing lot more difficult). – Ped7g Aug 27 '19 at 14:18