如何将数据从一个活动(意图)发送到另一个活动?
我使用此代码发送数据:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
如何将数据从一个活动(意图)发送到另一个活动?
我使用此代码发送数据:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
当前回答
如果在FragmentActivity中使用,请尝试以下操作:
第一页扩展了FragmentActivity
Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);
在片段中,您只需要首先调用getActivity(),
第二页扩展片段:
String receive = getActivity().getIntent().getExtras().getString("name");
其他回答
这是针对适配器的,对于您只需要更改mContext的活动对于片段,您需要将mContext更改为获取活动()
public static ArrayList<String> tags_array ;// static array list if you want to pass array data
public void sendDataBundle(){
tags_array = new ArrayList();
tags_array.add("hashtag");//few array data
tags_array.add("selling");
tags_array.add("cityname");
tags_array.add("more");
tags_array.add("mobile");
tags_array.add("android");
tags_array.add("dress");
Intent su = new Intent(mContext, ViewItemActivity.class);
Bundle bun1 = new Bundle();
bun1.putString("product_title","My Product Titile");
bun1.putString("product_description", "My Product Discription");
bun1.putString("category", "Product Category");
bun1.putStringArrayList("hashtag", tags_array);//to pass array list
su.putExtras(bun1);
mContext.startActivity(su);
}
如果您试图获取碎片中的额外数据,则可以尝试使用:
放置数据时使用:
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);
}
加起来
设置数据
String value = "Hello World!";
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("sample_name", value);
startActivity(intent);
获取数据
String value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("sample_name");
}
你也可以这样做//将价值放在意图上
Intent in = new Intent(MainActivity.this, Booked.class);
in.putExtra("filter", "Booked");
startActivity(in);
//从意图中获得价值
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String filter = bundle.getString("filter");
您可以从intent中获取任何类型的额外数据,无论是对象、字符串还是任何类型的数据。
Bundle extra = getIntent().getExtras();
if (extra != null){
String str1 = (String) extra.get("obj"); // get a object
String str2 = extra.getString("string"); //get a string
}
最短的解决方案是:
Boolean isGranted = getIntent().getBooleanExtra("tag", false);