I thought requestCode was some kind of indentifier for which Intent was just called for or something like that, please correct me if i'm wrong. But if this is the case, why is that if the requestCode for my pendingintent is 2, it acts differently when I have pass the requestcode as 0. I don't have another pendingintent in which is passed 2 as the requestcode.
Say I start my app and then I get a notification, when I click this notification ...
requestCode = 2 ...a new activity is created and brought to the foreground, when I press the back button I go back to the activity I was on in the same state before backing out of the app
requestCode = 0 ...I go straight back to the activity I was on in the same state before backing out of the app. No new activity is created
This isn't really a big issue, I could just pass 0 and the app will work how I want it to but I just wanna know why this happens.
In MainActivity.kt
private fun startAlarm (cal : Calendar){
val alarmManager :AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent (this, AlertReceiver().javaClass)
val pendingIntent : PendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.timeInMillis, pendingIntent)
}
}
AlertReceiver.kt
class AlertReceiver(): BroadcastReceiver() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onReceive(context: Context?, intent: Intent?) {
val notificationObj : Notification = Notification(context)
val notif : NotificationCompat.Builder = notificationObj.createNotification()
notificationObj.getManager().notify(1, notif.build())
}
}
In Notification.kt
fun createNotification(): NotificationCompat.Builder{
val main: Intent = Intent (this, MainActivity().javaClass).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
//-------------------talking about this line--------------------------------
val pendingMain: PendingIntent = PendingIntent.getActivity(this, 2, main, 0)
val notif : NotificationCompat.Builder = NotificationCompat.Builder(applicationContext, channelID)
.setSmallIcon(R.drawable.ic_android_black_24dp)
.setContentTitle("test:")
.setContentText("testing")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingMain)
.setAutoCancel((true))
return notif
}
When you are passing data from multiple Fragments to an Activity using onActivityResult(), that Activity identifies the child Fragment via request code