捆绑活动之间失去价值 [英] Bundle loses values between activities

查看:39
本文介绍了捆绑活动之间失去价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的第一个活动中,我想将两个String数组列表传递给另一个活动,但是由于某种原因,当我从第二个活动中提取值时,捆绑包会丢失所有值.以下是发送的相关代码:

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

现在,在第一个代码段中创建通知后,我检查以确保"friendNames"数组列表确实包含正确的值,然后调用 b.containsKey("fName),它返回true,但是当我在第二段代码中检查名称"数组列表时,没有值存在,并且当我调用 b.containsKey("fName"),它返回false.知道我在做什么错吗?

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?

推荐答案

尝试

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

代替

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

请参阅此链接. Intent.getExtras()始终返回null

尝试

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

代替

intent.putExtras(b);

然后尝试

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

代替

 b = getIntent().getExtras();

这篇关于捆绑活动之间失去价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆