【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
如何从一个活动(意图)向另一个活动发送数据?
我使用以下代码发送数据:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
#1楼
// How to send value using intent from one class to another class
// class A(which will send data)
Intent theIntent = new Intent(this, B.class);
theIntent.putExtra("name", john);
startActivity(theIntent);
// How to get these values in another class
// Class B
Intent i= getIntent();
i.getStringExtra("name");
// if you log here i than you will get the value of i i.e. john
#2楼
我刚刚在这里发布了一个涵盖该主题的答案,其中包括一些替代方法。
它利用了Vapor API ,这是我编写的简化jQuery开发的新jQuery启发式Android框架。 查看该答案中的示例,了解如何轻松在活动之间传递数据。
它还演示了VaporIntent
,它使您可以链接方法调用并利用重载的.put(...)
方法:
$.Intent().put("data", "myData").put("more", 568)...
您可以使用Vapor API轻松在整个应用程序中传递数据,因此希望对您和其他人在应用程序开发过程中有所帮助。
#3楼
如果您想获取碎片中的额外数据,则可以尝试使用:
使用以下方式放置数据:
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER);
使用以下方法获取数据:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getArguments().getInt(ARG_SECTION_NUMBER);
getArguments().getString(ARG_SECTION_STRING);
getArguments().getBoolean(ARG_SECTION_BOOL);
getArguments().getChar(ARG_SECTION_CHAR);
getArguments().getByte(ARG_SECTION_DATA);
}
#4楼
如果在FragmentActivity中使用,请尝试以下操作:
第一页扩展了FragmentActivity
Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);
在片段中,您只需要先调用getActivity()
,
第二页扩展了Fragment :
String receive = getActivity().getIntent().getExtras().getString("name");
#5楼
不必初始化另一个新的Intent来接收数据,只需执行以下操作:
String id = getIntent().getStringExtra("id");
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/3147623