我使用带有三个片段菜单的内置导航抽屉活动,我希望通过这些片段进行通信意味着想要将数据从一个传递到另一个。我发现有三种可能的方式来与片段进行通信。我也清楚地知道片段永远不会直接沟通。
现在我的问题是哪个是与片段进行通信的最佳方式,目前我使用second method
,我将所有这些对象放入Globalized objects which extends Application Class
。(getter& setter class)。 这是正确的方法吗?
答案 0 :(得分:7)
您可以在Object类中实现Serializable,然后只使用bundle传递它。我假设你正在从first_fragment中启动second_fragment。
在你的第一个片段中:
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
Fragment2 fragment2 = new Fragment2();
Bundle bundle = new Bundle();
YourObj obj = SET_YOUR_OBJECT_HERE;
bundle.putSerializable("your_obj", obj);
fragment2.setArguments(bundle);
ft.replace(android.R.id.content, fragment2);
ft.addToBackStack(null);
ft.commit();
在片段二中:
Bundle bundle = getArguments();
YourObj obj= (YourObj) bundle.getSerializable("your_obj");
修改强>
要序列化您的对象,只需在Object类中实现Serializable。
如果您的Object类是YourObj.class
public class YourObj implements Serializable {
int id;
String name;
// GETTERS AND SETTERS
}
答案 1 :(得分:5)
通过使模型类Serializable或Parcelable可以在fragment和Activities之间传递对象。
Parcelable是一个Android类,可以支持更复杂的类序列化。 在这里检查Parceble类的实现:http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
Serializable是一个Java类,适用于小对象。 对于Serializable,请访问: https://developer.android.com/reference/java/io/Serializable.html
假设您的模型类是NewObject,然后在片段类中使用以下内容:
Fragment fragment = new Fragment();
NewObject newObject = new NewObject();
Bundle bundle = new Bundle();
bundle.putParcelable("Object", newObject);
fragment.setArguments(bundle);
要在onCreate函数中使用另一个片段中的bundle来获取它:
NewObject newObject = (NewObject) bundle.getParcelable("Object");
答案 2 :(得分:3)
数据持有者类:
public class DataHolder implements Serializable{
private String name,id;
public DataHolder(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
FragmentA:
DataHolder dataholder=new DataHolder("1","TestName");
Bundle bundle=new Bundle();
bundle.putSerializable("obj",dataholder);
Fragment fragment=new FragmentB();
fragment.setArguments(bundle);
fragmentManager = getActivity(). getSupportFragmentManager();
fragmentTransaction = fragmentManager .beginTransaction();
fragmentTransaction.add(R.id.container, fragment);
fragmentTransaction.commit();
FragmentB:
DataHolder dataholder = (DataHolder)getArguments().getSerializable(obj);
答案 3 :(得分:2)
在托管这些片段的Activity中,定义一个变量
User
现在,在您的片段中,当您从Api获得响应时,请在Activity中初始化变量@Override
public void onClick(View view) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, getString(R.string.RESTAPI_URL), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Response: ",response.toString());
Gson gson = new Gson();
User user = gson.fromJson(String.valueOf(response),User.class);
// initialise User variable in Home Activity
((HomeActivity)getActivity()).mUser = user;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ErrorResponse: ",error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(jsonObjectRequest);
,如下所示:
User
}
假设您的Serializable
类实现了Parcelable接口,如果没有,那么只需将该类设为Parcelable即可。这与Java中的Parcelable
相同,但Android
针对SecondFragment secondFragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("ARG_USER", user);
secondFragment.setArguments(bundle);
//...code for loading second fragment
进行了优化。
最后,当您加载第二个片段时,只需将User对象传递给第二个片段,如下所示:
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|yml)$": "<rootDir>/src/__mocks__/fileMock.js",
...
}
答案 4 :(得分:1)
当您需要在片段之间传递有限数据时,可以使用Bundle Instance。如果你有复杂的对象传递你更喜欢接口而不是其他方式。你也可以查看reff的链接
答案 5 :(得分:1)
您可以尝试这种方式。 在您的第一个片段中
Outlet outlet=New Outlet;
Bundle bundle = new Bundle();
bundle.putSerializable("outlet",outlet);
Fragment frag=null;
frag=new Outlet_Edit();
if(frag!=null){
frag.setArguments(bundle);
FragmentManager fragmentManager=getActivity().getSupportFragmentManager();
FragmentTransaction ft=fragmentManager.beginTransaction();
ft.replace(R.id.sacreenarea,frag);
ft.commit();
}
在第二个片段中
//second Fragmnet
Outlet editing_outlet=(Outlet) getArguments().getSerializable("outlet");
Log.d("Editing Outlet",editing_outlet.toString());
对象应该是这种方式。
public class Outlet implements Serializable {
String id;
String shopname;
String shopowner;
String address;
String contact;
String marketid;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getShopname() {
return shopname;
}
public void setShopname(String shopname) {
this.shopname = shopname;
}
public String getShopowner() {
return shopowner;
}
public void setShopowner(String shopowner) {
this.shopowner = shopowner;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getMarketid() {
return marketid;
}
public void setMarketid(String marketid) {
this.marketid = marketid;
}
@Override
public String toString() {
return "Outlet{" +
"id='" + id + '\'' +
", shopname='" + shopname + '\'' +
", shopowner='" + shopowner + '\'' +
", address='" + address + '\'' +
", contact='" + contact + '\'' +
", marketid='" + marketid + '\'' +
'}';
}
}