![](/img/trans.png)
[英]What does requestCode and resultCode in onActivityResult refer to?
[英]What does requestCode represent?
我認為 requestCode 是某種標識符,僅需要 Intent 或類似的東西,如果我錯了,請糾正我。 但是如果是這種情況,為什么如果我的pendingintent 的requestCode 是2,當我將requestcode 傳遞為0 時,它的行為會有所不同。我沒有另一個pendingintent,其中將2 作為requestcode 傳遞。
假設我啟動我的應用程序,然后我收到一個通知,當我單擊此通知時......
requestCode = 2 ...創建一個新活動並將其帶到前台,當我按下后退按鈕時,我 go 回到我在同一個 state 中進行的活動,然后退出應用程序
requestCode = 0 ...I go 直接回到我在同一個 state 中進行的活動,然后退出應用程序。 未創建新活動
這不是一個真正的大問題,我可以通過 0 並且該應用程序將按照我想要的方式運行,但我只想知道為什么會發生這種情況。
在 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)
}
}
警報接收器.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())
}
}
在 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
}
當您使用 onActivityResult() 將數據從多個片段傳遞到活動時,該時間活動通過請求代碼識別子片段。 我希望你能明白我想說什么
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.