Reputation: 9
I have developed an app where on one screen the user scans some barcodes in sets - maybe 2 in one set, maybe 3 or 4 in another. These barcodes are quite a long way apart, eg. the first one in a set may be at the front of a building and then others round the back of the building, etc. Sometimes they get called away to another site before finishing a building, and have to move to another screen in the same app and do something there. When they come back to the half-scanned building and redisplay the original screen, they want the scans already done in that set to be redisplayed so they only have to finish the set. I have looked at 'protected override void OnSaveInstanceState(Bundle savedInstanceState)' so when they change screens this code runs:
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();
}
This seems to run OK - the toast pops up as expected. However, when the user returns to the bar code scanning screen, these values don't reappear. Here is my code:
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();
}
While I am developing the app, when I return to the bar code screen the toast says 'savedInstanceState IS null' and the two TextViews do not display the expected values. I have put the 'if (savedInstanceState != null)' block at the foot of the 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();
}
Have I put the 'if (savedInstanceState != null)' block in the wrong place? Where should it go if so? Have I missed anything else?
Thank you in advance.
Upvotes: 0
Views: 48
Reputation: 4342
I created a project and tested the InstanceState
. It shows the same situation happened to you.
I searched relevant cases about it. You can refer to this: How can I save an activity state using the save instance state?.
If it's ok, Intent may be an alternative.
Upvotes: 0