在我的第一個活動中,我想將兩個字符串數組列表傳遞給另一個活動,但由於某種原因,當我從第二個活動中提取值時,該包會丟失所有值。下面是發送相關代碼:Bundle在活動之間丟失了值
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Bundle b = new Bundle();
b.putStringArrayList("fName", friendNames);
b.putStringArrayList("fIds", friendIds);
Intent intent = new Intent(getApplicationContext(), Friendrequest.class);
PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT).cancel();
intent.putExtras(b);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// creates notification
Notification n = new Notification.Builder(this)
.setContentTitle("You have a friend request!")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).setAutoCancel(true).build();
notificationManager.notify(0, n);
下面是用於接收的相關代碼:
names = new ArrayList<String>();
ids = new ArrayList<String>();
Bundle b = new Bundle();
b = getIntent().getExtras();
names = b.getStringArrayList("fName");
ids = b.getStringArrayList("fIds");
現在,經過創建我的第一個代碼段的通知,我檢查,以確保「friendNames」數組列表確實包含正確的值,我打電話給b.containsKey("fName")
,它返回true,但只要我檢查代碼的第二個片段中的「names」數組列表,沒有任何值在那裏,並且當我撥打b.containsKey("fName")
時,它返回false。任何想法我做錯了什麼?
嗯,仍然沒有骰子。我加了'b = getIntent()。getBundleExtra(「android.intent.extra.INTENT」);我在「Log」行上得到了一個空指針異常。 – joebro
嘗試names = savedInstanceState.getStringArrayList(「fName」); – Prem
沒有骰子。我用我的原始代碼和您的第一個建議嘗試了您的最新建議。有什麼,我失蹤,因爲意圖是等待意圖的一部分? – joebro