我想在有人关闭应用时保存此活动的状态。它只包含一个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);
}
}
}
}
}
这是我保存状态以进行主动活动的地方,它似乎保留了我的列表活动和主要活动的数据。我认为我只需要为应用程序销毁时做同样的事情。
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);`
答案 0 :(得分:2)
当用户关闭应用时是什么意思?你的意思是当他关闭它片刻并再次打开它或者应用程序完全关闭时?
如果你的意思是第二个,你应该使用OnDestroy来保存&#34;下一次OnCreate上的状态和加载。您将不得不手动创建保存和加载过程(使用数据库?SharedPreferences?它取决于您)
Check also here用于活动生命周期。
也总是发布你到目前为止所尝试的内容并且它不想要你想要的确切结果
在OnDestroy上存储值的一种简单方法是使用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
使用以下方式访问已保存的值:
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Application.Context);
_bool = prefs.GetBoolean ("keyName");
_int = prefs.GetInt ("keyName");
_string = prefs.GetString ("keyName");