我想将其他活动的onClick事件中的数据传递给之前活动的FRAGMENT。我可以看到日志,它显示从活动到主要活动的值,但不能传递给主要活动的片段。
我已经参考了这个链接: Send string from an activity B to a fragment F of activity A
但我是一个初学者并且有一个愚蠢的头脑,我尝试过但无法得到预期的结果,最重要的是,我并不真正理解解决方案的想法。
我对界面的使用感到困惑,尽管在解决此问题时我已经阅读了很多来源和案例。或者我可以使用其他解决方案来实现此功能吗?
感谢您的帮助。
主要活动:
Fragment_step_1 getHashMapStep1 = new Fragment_step_1();
Fragment_step_2 getHashMapStep2 = new Fragment_step_2();
......
public void onCreate(Bundle savedInstanceState) {
......
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
Bundle extrasFrag2 = getIntent().getExtras();
boolean getFromConfirmPage = getIntent().getBooleanExtra("checkReturn", fromConfirm);
Log.e("boolean", String.valueOf(getFromConfirmPage));
if(extras == null) {
getEmailFromID = null;
} else {
if(getFromConfirmPage){
strFullName = extras.getString("ConfirmFullName");
strIDNum = extras.getString("ConfirmIDNum");
strDOB = extras.getString("ConfirmDOB");
strPhone = extras.getString("ConfirmPhone");
strEmail = extras.getString("ConfirmEmail");
strAddress = extras.getString("ConfirmAddress");
strPlanPrice = extras.getString("plan_price");
strPlan_type = extras.getString("ConfirmPlanType");
Bundle argsFrag1 = getHashMapStep1.getArguments();
Bundle argsFrag2 = getHashMapStep2.getArguments();
argsFrag1.putString("ConfirmFullName", strFullName);
argsFrag1.putString("ConfirmIDNum", strIDNum);
argsFrag1.putString("ConfirmPhone", strPhone);
argsFrag1.putString("ConfirmEmail", strEmail);
argsFrag2.putString("ConfirmDOB", strDOB);
argsFrag2.putString("ConfirmAddress", strAddress);
getHashMapStep1.setArguments(argsFrag1);
getHashMapStep2.setArguments(argsFrag2);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentPersonal, getHashMapStep1);
transaction.replace(R.id.fragment_correspondence, getHashMapStep2);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
} else {
getEmailFromID = extras.getString("Email ID");
strPlan_type = extras.getString("Plan_Type");
strPlanPrice = extras.getString("Plan_Price");
}
}
......
else {
Intent confirmInsure = new Intent(/*main_activity*/.this, /*activity_with_edit_btn*/.class);
Bundle extras = new Bundle();
extras.putString("ConfirmFullName", strFullName);
extras.putString("confirmDOB", strDOB);
extras.putString("ConfirmIDNum", strIDNum);
extras.putString("ConfirmPhone", strPhone);
extras.putString("ConfirmEmail", strEmail);
extras.putString("ConfirmAddress", strAddress);
extras.putString("plan_type", strPlan_type);
extras.putString("plan_price", strPlanPrice);
confirmInsure.putExtras(extras);
startActivity(confirmInsure);
}
}
......
}
活动与"编辑"按钮:
......
btn_editInfo.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent confirmInsure = new Intent(/*activity_with_edit_btn*/.this, /*To_main_activity*/.class);
Bundle extras = new Bundle();
extras.putString("ConfirmFullName", CustName);
extras.putString("ConfirmIDNum", IDNum);
extras.putString("ConfirmDOB", strDOB);
extras.putString("ConfirmPhone", PhoneNo);
extras.putString("ConfirmEmail", email);
extras.putString("ConfirmAddress", rAddress);
extras.putString("ConfirmPlanType", formatPlanType);
extras.putString("plan_price", planPrice);
extras.putBoolean("checkReturn", fromConfirm);
confirmInsure.putExtras(extras);
startActivity(confirmInsure);
}
});
......
片段1& 2:
......
@Override //SHOULD I GET VALUES ON THIS METHOD?
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Bundle args = this.getArguments();
if (args != null) {
strName = args.getString("ConfirmFullName");
strHKID = args.getString("ConfirmIDNum");
strTel = args.getString("ConfirmPhone");
strEmail = args.getString("ConfirmEmail");
name.setText(strName);
HKIDNumber.setText(strHKID);
getEmail.setText(strEmail);
phone.setText(strTel);
}
//REMAIN TWO VALUES ARE GETTING ON FRAGMENT_2
}
......
答案 0 :(得分:1)
您可以使用DataHolder(Singleton)来保存值。您可以从第二个活动中设置值,您可以在您的片段中(或在整个应用程序中 - 您想要的任何地方)。 Singletons在整个应用程序中都有一个实例。它可以在整个应用程序中保留。您可以从活动,片段,接口等访问它们。保存数据并在activity-fragment,fragment-activity,fragment-fragment之间传递数据会很有帮助。
创建DataHolder.java
public class DataHolder {
private static DataHolder mInstance;
private String name, idNumber, dob, address, planType; //fields you want
//add all the fields you want--like email,phoneNUmber,etc.,
public static DataHolder getInstance() {
if (mInstance == null)
return mInstance = new DataHolder();
else
return mInstance;
}
//getters and setters for the fields
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPlanType() {
return planType;
}
public void setPlanType(String planType) {
this.planType = planType;
}
//define getters and setters for all defined fields
}
在你的Activity2中,
DataHolder holder;
btn_editInfo.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
holder = DataHolder.getInstance(); //Getting DataHolder Instance
holder.setName(CustName);
holder.setIdNumber(IDNum);
//set values for all fields using setters
//holder.setSomething(something);
//Start your first activity
Intent confirmInsure = new Intent(/*activity_with_edit_btn*/.this, /*To_main_activity*/.class);
startActivity(confirmInsure);
}});
在你的片段1和2中,
DataHolder holder;
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
holder = DataHolder.getInstance(); //get instance of DataHolder
name.setText(holder.getName()); //get customer name by getName()
// do the same for all fields
HKIDNumber.setText(holder.getIdNumber());
getEmail.setText(holder.getEmail());
phone.setText(holder.getPhNumber());
}
希望它会对你有所帮助。