我有三个活动A,B和C。我使用StartForResult()将值从A传递到B,然后再次使用相同的startforResult()将值从B传递给C。该值在活动B中已更新,我知道更新值是通过再次单击该按钮或再次打开该应用程序。但是,我需要显示的是从C传递值后的电导率B。如何解决此问题?会很大的帮助。因为我的代码是:
活动A
public void okClick()
{
Intent updatevalue = new Intent(this, typeof(HoldingRegister));
updatevalue.PutExtra("passclickedvalue", clickedvalue);
updatevalue.PutExtra("PassParameters", textBox1.Text);
SetResult(Result.Ok, updatevalue);
Finish();
}
活动B
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
int ValuedClicked = data.GetIntExtra("passclickedvalue", 0);
string enterdata = data.GetStringExtra("PassParameters");
PassUpdatedValues(enterdata, ValuedClicked);
}
}
public void PassUpdatedValues(string datas, int ValuedClicked)
{
double val = Convert.ToDouble(datas);
bool res = double.TryParse(datas, out val);
if (ValuedClicked == 2)
{
if (res == true && val > -1 && val < 2)
{
lblDensityCorrectionHR.Text = datas;
Intent HRvalue = new Intent(this, typeof(ToolBarMainActivity));
HRvalue.PutExtra("passclickedvalue", ValuedClicked);
HRvalue.PutExtra("PassParameters", datas);
SetResult(Result.Ok, HRvalue);
Finish();
}
}
}
活动c
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
ValuedClicked = data.GetIntExtra("passclickedvalue", 0);
enterdata = data.GetStringExtra("PassParameters");
if (ValuedClicked == 2)
{
Density(enterdata);
}
}
public void Density(string datas)
{
int addr = 0x13;
int cmdNo = 03;
ParamWrite(addr, true);
ListGlobalFrame.Clear();
if (RsTransmitRec(cmdMake(cmdNo, CMD_RD), modestate) == true) {
if (DivRecData(GetRecData, cmdNo) == true) //Response separation
{
Intent ReturnHRvalue = new Intent(this, typeof(HoldingRegister));
ReturnHRvalue.PutExtra("passclickedvalue", ValuedClicked);
ReturnHRvalue.PutExtra("PassParameters", RecDt.confDCorretion.ToString());
StartActivityForResult(ReturnHRvalue, 0);
}
else
{
MsgLogDispSet("Invalid Response Data。", Android.Graphics.Color.Red);
}
}
}
答案 0 :(得分:0)
问题原因: 您使用StartForResult()方法的方式不正确
分辨率: 通过单击活动B上的按钮打开活动C时,如果要在从活动C中重试时获得结果,则需要使用StartForResult()方法。同时,您需要在Acitivity C中使用SetResult()并在Activity B中实现OnActivityResult()接口。
我不太确定您的逻辑,因此我编写了一个简单的演示来描述StartForResult的用法:
演示:
ActivityA:
public class ActivityA : Activity
{
Button button1;
EditText textBox1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
button1 = FindViewById<Button>(Resource.Id.button1);
textBox1 = FindViewById<EditText>(Resource.Id.editText1);
button1.Click += delegate {
//Call Your Method When User Clicks The Button
Intent updatevalue = new Intent(this, typeof(ActivityB));
updatevalue.PutExtra("PassParameters", textBox1.Text);
StartActivity(updatevalue);
};
}
}
ActivityB:
public class ActivityB : Activity
{
EditText edittext2;
Button button2;
string valueFromA;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout2);
valueFromA = Intent.Extras.GetString("PassParameters");
edittext2 = FindViewById<EditText>(Resource.Id.editText2);
edittext2.Text = valueFromA;
button2 = FindViewById<Button>(Resource.Id.button2);
button2.Click += delegate {
//Call Your Method When User Clicks The Button
Intent updatevalue = new Intent(this, typeof(ActivityC));
updatevalue.PutExtra("PassParameters", edittext2.Text);
StartActivityForResult(updatevalue, 0);
};
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
string dataFromC = data.GetStringExtra("PassParameters");
edittext2.Text = dataFromC;
}
}
}
ActivityC:
public class ActivityC : Activity
{
EditText edittext3;
Button button3;
string valueFromB;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout3);
valueFromB = Intent.Extras.GetString("PassParameters");
edittext3 = FindViewById<EditText>(Resource.Id.editText3);
edittext3.Text = valueFromB;
button3 = FindViewById<Button>(Resource.Id.button3);
button3.Click += delegate {
Intent HRvalue = new Intent(this, typeof(ActivityB));
HRvalue.PutExtra("PassParameters", edittext3.Text);
SetResult(Result.Ok, HRvalue);
Finish();
};
}
}