onNewIntent() method called multiple times

33 Views Asked by At

I have below requirement. I want to create notification. On tap of notification app should open and navigate to specific fragment based on data received from intent extras. I need to handle this irrespective of app is in killed state or in foreground.

I have singleTop launch mode set in manifest.

APP KILLED STATE SCENARIO:

It is working well if app is in killed state, it calls onCreate() method and I have screen handling logic in it as well. But onNewIntent() gets called multiple times thereafter

APP FOREGROUND SCENARIO:

If app is in foreground state then onNewIntent() gets called but without intent extras data.

Please find below code.

To create notification,

                notificationIntent.flags = (Intent.FLAG_ACTIVITY_CLEAR_TOP
                        or Intent.FLAG_ACTIVITY_SINGLE_TOP)
                notificationIntent.putExtra(Constants.MEDICATION_EXTRA, titleText)
                contentIntent = PendingIntent.getActivity(
                    context, 0,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
                )
    
               val id = notificationHelper.postNotification(
                notificationHelper.createNotification(
                    contentText = contentText,
                    contentIntent = contentIntent,
                    autoCancel = true,
                    title = titleText,
                )
            )
1

There are 1 best solutions below

0
SaurabhG On

The cause of the issue was that I did not set intent before using it directly.

I was using intent via Acitivity's getter method getIntent(). If I am using intent via the getter method I must set intent received in onNewIntent(intent:Intent) method.