There are some editText controls in my UI, I hope these input values can be restored after I restart the UI, and more I need to use the saved values in my customize service.
I know I can use SharedPreferences to do that , but it's too complex if I have many controls. Is there a simple way?
I don't know if I can get saved value using savedInstanceState http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
private static String GetStringSharedPreferences(Context mContext,String key){
SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(mContext);
return prefs.getString(key, "");
}
private static void SaveStringSharedPreferences(Context mContext,String key,String input){
SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, input);
editor.commit();
}
答案 0 :(得分:1)