如何将数据从片段发送到另一个活动? [英] How to send data from fragment to another activity?

查看:62
本文介绍了如何将数据从片段发送到另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据从Fragment发送到另一个活动

I need to send data from Fragment to another activity

我正在HomeActivity下的LoadsFragment中使用此代码

I am using this code in my LoadsFragment under HomeActivity

Intent intent = new Intent(activity, LoadActivity.class);
intent.putExtra("loadsPosition",position);
activity.startActivity(intent);

在另一个活动(LoadActivity)中接收数据

in another activity(LoadActivity) to receive data

Intent intent=getIntent();    
String loadsPosition = intent.getStringExtra("loadsPosition");

但意图没有附加内容

查看下面的屏幕截图

推荐答案

但意图没有附加内容

but intent has no Extras

您的屏幕截图显示确实如此...

Your screenshot says it does...

您的第二个屏幕截图显示了processIntent(Bundle bundle),但是第三个屏幕截图显示了processIntent(Intent intent),因此您应该弄清楚哪个不起作用.但是两个Bundles都不为空.

Your second screenshot shows processIntent(Bundle bundle), but the third shows processIntent(Intent intent), so you should clarify which isn't working. But both Bundles are not null.

片段具有它们的自己的startActivity方法.您只需要父Activity即可创建Intent

Fragments have their own startActivity method. You only need the parent Activity to create the Intent

Intent intent = new Intent(getActivity(), LoadActivity.class);
intent.putExtra("loadsPosition",position);
startActivity(intent);

最重要的是,您的位置是整数,但是您试图将其作为字符串获取,因此该字符串将为空

Most importantly, your position is an integer, but you're trying to get it as a string, therefore the string will be null

Intent intent=getIntent();    
int loadsPosition = intent.getIntExtra("loadsPosition", -1);

Intent不应为null,并且应具有Bundle.如果此整数返回为-1,则返回了默认值,您应该使用较小的示例

The Intent should not be null, and it should have a Bundle. If this integer comes back as -1, then the default here has been returned, and you should debug some more with a smaller example

这篇关于如何将数据从片段发送到另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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