我开发了一个应用程序,用户可以在一个屏幕上成组扫描一些条形码——一组可能有 2 个,另一组可能有 3 或 4 个。这些条形码相距很远,例如。一组中的第一个可能在建筑物的前面,然后其他人在建筑物的后面,等等。有时他们在完成建筑物之前被叫到另一个站点,并且必须移动到同一应用程序中的另一个屏幕并在那里做点什么。当他们返回扫描到一半的建筑物并重新显示原始屏幕时,他们希望重新显示该组中已经完成的扫描,因此他们只需完成该组即可。 我看过“protected override void OnSaveInstanceState(Bundle savedInstanceState)”所以当他们改变屏幕时,这段代码运行:
protected override void OnSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.PutString("gstrThisSiteID", gstrThisSiteID);
savedInstanceState.PutString("gstrThisSiteName", gstrThisSiteName);
savedInstanceState.PutString("gstrThisSiteNameFromUnfinishedSiteScan", gstrThisSiteNameFromUnfinishedSiteScan);
savedInstanceState.PutString("gstrListOfScansDoneForThisSiteAdd", gstrListOfScansDoneForThisSiteAdd);
savedInstanceState.PutString("gstrListOfScansForThisSiteRemove", gstrListOfScansForThisSiteRemove);
savedInstanceState.PutString("gstrThisTagIDOrBarCodeValue", gstrThisTagIDOrBarCodeValue);
//Always call the base implementation
base.OnSaveInstanceState(savedInstanceState);
Toast.MakeText(this, "OnSaveInstanceState(Bundle savedInstanceState)", ToastLength.Long).Show();
}
这似乎运行正常 - 吐司按预期弹出。 但是,当用户返回条码扫描屏幕时,这些值不会重新出现。这是我的代码:
if (savedInstanceState != null)
{
//_counter = bundle.GetInt("click_count", 0);
Toast.MakeText(this, "savedInstanceState != null", ToastLength.Long).Show();
gstrThisSiteID = savedInstanceState.GetString("gstrThisSiteID", "");
gstrThisSiteName = savedInstanceState.GetString("gstrThisSiteName", "");
gstrThisSiteNameFromUnfinishedSiteScan = savedInstanceState.GetString("gstrThisSiteNameFromUnfinishedSiteScan", "");
gstrListOfScansDoneForThisSiteAdd = savedInstanceState.GetString("gstrListOfScansDoneForThisSiteAdd", "");
gstrListOfScansForThisSiteRemove = savedInstanceState.GetString("gstrListOfScansForThisSiteRemove", "");
gstrThisTagIDOrBarCodeValue = savedInstanceState.GetString("gstrThisTagIDOrBarCodeValue", "");
CPsWaitingTextView = FindViewById<TextView>(Resource.Id.CPsWaitingTextView_id);
CPsWaitingTextView.SetTextSize(Android.Util.ComplexUnitType.Dip, (float)20);
CPsWaitingTextView.SetTextColor(Android.Graphics.Color.White);
CPsWaitingTextView.Text = "strCheckpointsForThisSite";
CPsWaitingTextView.MovementMethod = new Android.Text.Method.ScrollingMovementMethod();
CPsScannedTextView = FindViewById<TextView>(Resource.Id.CPsScannedTextView_id);
CPsScannedTextView.Text = gstrThisSiteName;
}
else
{
Toast.MakeText(this, "savedInstanceState IS null", ToastLength.Long).Show();
}
在我开发应用程序时,当我返回到条形码屏幕时,toast 显示“savedInstanceState IS null”并且两个 TextView 未显示预期值。 我已将“if (savedInstanceState != null)”块放在 OnCreate void 的脚下:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.TagAndBarcodeLayout);
//Many lines of code setting up the screen etc, etc.
//.............
if (savedInstanceState != null)
{
//_counter = bundle.GetInt("click_count", 0);
Toast.MakeText(this, "savedInstanceState != null", ToastLength.Long).Show();
gstrThisSiteID = savedInstanceState.GetString("gstrThisSiteID", "");
gstrThisSiteName = savedInstanceState.GetString("gstrThisSiteName", "");
gstrThisSiteNameFromUnfinishedSiteScan = savedInstanceState.GetString("gstrThisSiteNameFromUnfinishedSiteScan", "");
gstrListOfScansDoneForThisSiteAdd = savedInstanceState.GetString("gstrListOfScansDoneForThisSiteAdd", "");
gstrListOfScansForThisSiteRemove = savedInstanceState.GetString("gstrListOfScansForThisSiteRemove", "");
gstrThisTagIDOrBarCodeValue = savedInstanceState.GetString("gstrThisTagIDOrBarCodeValue", "");
CPsWaitingTextView = FindViewById<TextView>(Resource.Id.CPsWaitingTextView_id);
CPsWaitingTextView.SetTextSize(Android.Util.ComplexUnitType.Dip, (float)20);
CPsWaitingTextView.SetTextColor(Android.Graphics.Color.White);
CPsWaitingTextView.Text = "strCheckpointsForThisSite";
CPsWaitingTextView.MovementMethod = new Android.Text.Method.ScrollingMovementMethod();
CPsScannedTextView = FindViewById<TextView>(Resource.Id.CPsScannedTextView_id);
CPsScannedTextView.Text = gstrThisSiteName;
}
else
{
Toast.MakeText(this, "savedInstanceState IS null", ToastLength.Long).Show();
}
我是否将“if (savedInstanceState != null)”块放在了错误的位置?如果是应该去哪里?我还错过了什么吗?
提前谢谢你。