2

I want to save the state of this activity when someone closes the app. It just contains a ListActivity simplelistitemchecked...

   namespace XamarinScanner
     {
      [Activity(Label = "@string/scanHistory", ScreenOrientation = ScreenOrientation.Portrait)]
     public class ScanHistoryActivity : ListActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        var codes = Intent.Extras.GetStringArrayList("Codes");

        codes.ToList();
        ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemChecked, codes);
        ListView lv = FindViewById<ListView>(Android.Resource.Id.List);
        lv.ChoiceMode = ChoiceMode.Multiple;

        foreach (var c in codes)
        {
            if (c.Contains("Success"))
            {
                int position = codes.IndexOf(c);
                lv.SetItemChecked(position, true);
            }
        }
    }
}

}

this is where i save my state for my mainactivity, it seems to keep data for my listactivity and main activity when backgrounded. I think i just need to do the same thing for when app is destroyed..

    protected override void OnSaveInstanceState(Bundle outState)'
       {
        outState.PutStringArrayList("Codes", _codes.ToArray());
        base.OnSaveInstanceState(outState);
    }
    protected override void OnCreate(Bundle bundle)
    {

        Url = "http://10.0.0.103:4321/Scan";
        if (bundle != null)
        {
            var c = bundle.GetStringArrayList("Codes");
           instance state");
        }
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);`
Sam Ra
  • 255
  • 6
  • 18
  • Did you even try looking for the answer yourself? This is not exactly an unusual thing to want to accomplish. http://stackoverflow.com/questions/151777/saving-activity-state-in-android – Levesque Sep 30 '15 at 14:54
  • Yes tried a few different times using different examples but no luck. Very new to xamarin, c# and code in general! – Sam Ra Sep 30 '15 at 14:59
  • Well, you won't have much luck getting help with such an open ended question. You'll need to explain exactly what you've tried and exactly what went wrong, otherwise you're just asking people to do your work for you. – Levesque Sep 30 '15 at 15:38
  • you cannot save state on OnDestroy, only if you create your version of OnSaveInstanceState – CDrosos Sep 30 '15 at 16:03

1 Answers1

2

What do you mean by when the user close the App? you mean when he close it for a moment and open it again or when the App is closed completely?
If you mean the 2nd, You should use OnDestroy to "save" the state and load in on OnCreate in the next time. You will have to manually create the save and load procedure (with database?SharedPreferences? it's up to you)
Check also here for Activity lifecycle.

Also always post what you have try so far and it didnt want and the exact results you were expecting

A simple way of storing a value on OnDestroy is with SharedPreferences:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Application.Context);
ISharedPreferencesEditor editor = prefs.Edit ();
editor.PutBoolean ("keyName", _bool);
// editor.Commit();    // applies changes synchronously on older APIs
editor.Apply();        // applies changes asynchronously on newer APIs

Access saved values using:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Application.Context);
_bool = prefs.GetBoolean ("keyName");
_int = prefs.GetInt ("keyName");
_string = prefs.GetString ("keyName");
CDrosos
  • 2,418
  • 4
  • 26
  • 49
  • please put code on your question, not in comments, comments are not for big discussions but for small corrections – CDrosos Sep 30 '15 at 15:53
  • where would I put that code? Is it not normal to save all app data when you close down your app? I'm very new to this! – Sam Ra Sep 30 '15 at 16:16
  • you can put this code on OnDestroy to save the data and on OnCreate to retrieve it. I don't know exactly your case but saving the App state usually is done with SharedPreferences or database or another way of storing the current value. I usually when i want the user to be able to save some state or data i will make a function for this (a save setting/button), i will not save it on OnDestroy – CDrosos Sep 30 '15 at 16:27