0

In my first activity I want to pass two String array lists to another activity, but for some reason when I go to pull the values from the second activity, the bundle loses all the values. Here is the relevant code for sending:

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

Here is the relevant code for receiving:

names = new ArrayList<String>();
ids = new ArrayList<String>();

Bundle b = new Bundle();

b = getIntent().getExtras();

names = b.getStringArrayList("fName");

ids = b.getStringArrayList("fIds");

Now, after I create my notification in the first snippet of code, I check to make sure that the "friendNames" array list does indeed contain the correct values and I make a call to b.containsKey("fName") and it returns true, but as soon as I check the "names" array list in the second snippet of code, none of the values are there and when I make a call to b.containsKey("fName") it returns false. Any idea what I am doing wrong?

Simulant
  • 19,190
  • 8
  • 63
  • 98
joebro
  • 581
  • 3
  • 11

3 Answers3

2

Try

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

instead of

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

Refer to this link. Intent.getExtras() always returns null

Try

intent.putExtra("android.intent.extra.INTENT", b);

instead of

intent.putExtras(b);

and try

b = getIntent().getBundleExtra("android.intent.extra.INTENT");

instead of

 b = getIntent().getExtras();
Community
  • 1
  • 1
Prem
  • 4,823
  • 4
  • 31
  • 63
  • Hmm, still no dice. I added `b = getIntent().getBundleExtra("android.intent.extra.INTENT"); Log.d("HERE",b.containsKey("fName")+"");` and I get a null pointer exception on the "Log" line. – joebro Dec 23 '13 at 22:01
  • Try names = savedInstanceState.getStringArrayList("fName"); – Prem Dec 23 '13 at 22:27
  • No dice. I tried your latest suggestion with my original code and with your first suggestion. Is there something that I'm missing because the intent is part of a pending intent? – joebro Dec 24 '13 at 00:09
  • Your latest edit of changing the pending intent worked perfectly. I stumbled on that right as I saw your edit. Thank you! – joebro Dec 24 '13 at 00:17
0

From your former activity (the one which sends the value) intent.putExtra("android.intent.extra.INTENT", b);

and access them on the second activity by intent.getExtra(b);

Akshat Agarwal
  • 2,837
  • 5
  • 29
  • 49
0

If you are exchanging custom class objects then the class must implement Parcelable. If you miss any member while doing writeString and readString etc. then the corresponding member will be lost.

a_fan
  • 383
  • 2
  • 22