joebro
joebro

Reputation: 589

Bundle loses values between activities

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?

Upvotes: 0

Views: 664

Answers (3)

a_fan
a_fan

Reputation: 387

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.

Upvotes: 0

Prem
Prem

Reputation: 4831

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

Upvotes: 2

Akshat Agarwal
Akshat Agarwal

Reputation: 2847

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

Upvotes: 0

Related Questions