如何从一个活动(意图)向另外一个活动发送数据? api
我使用如下代码发送数据: 框架
Intent i=new Intent(context,SendMessage.class); i.putExtra("id", user.getUserAccountId()+""); i.putExtra("name", user.getUserFullName()); context.startActivity(i);
// 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
我刚刚在这里发布了一个涵盖该主题的答案,其中包括一些替代方法。 this
它利用了Vapor API ,这是我编写的简化jQuery开发的新jQuery启发式Android框架。 查看该答案中的示例,了解如何轻松在活动之间传递数据。 spa
它还演示了VaporIntent
,它使您能够连接方法调用并利用重载的.put(...)
方法: code
$.Intent().put("data", "myData").put("more", 568)...
您能够使用Vapor API轻松在整个应用程序中传递数据,所以但愿对您和其余人在应用程序开发过程当中有所帮助。 开发
若是您想获取碎片中的额外数据,则能够尝试使用: get
使用如下方式放置数据: it
Bundle args = new Bundle(); args.putInt(DummySectionFragment.ARG_SECTION_NUMBER);
使用如下方法获取数据: io
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); }
若是在FragmentActivity中使用,请尝试如下操做: class
第一页扩展了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");
没必要初始化另外一个新的Intent来接收数据,只需执行如下操做:
String id = getIntent().getStringExtra("id");