150 likes | 226 Views
Cosc 5/4730. Android: “Dynamic ” data. Saving Dynamic data. While there are a lot of ways to save data Via the filesystem , database, etc. You can also save a “small” amount of dynamic data via a Bundle onCreate (Bundle savedInstanceState ) Temporary data sharedPreferences
E N D
Cosc 5/4730 Android: “Dynamic” data.
Saving Dynamic data. • While there are a lot of ways to save data • Via the filesystem, database, etc. • You can also save a “small” amount of dynamic data via a Bundle • onCreate(Bundle savedInstanceState) • Temporary data • sharedPreferences • More permanent data.
savedInstanceSate • Override • public void onSaveInstanceState(Bundle savedInstanceState) • void onRestoreInstanceState(Bundle savedInstanceState) • But they are not in the Activity Lifecycle, so it possible they will not get called! • Use Oncreate() as well.
Another note. • Configuration Changes • Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed and then restarted. • See note at the end of this lecture about orientation. • See Configuration Changes on • http://developer.android.com/reference/android/app/Activity.html
SavedInstancevsSharePreferences • onSaveInstanceState(Bundle) activity event • This data will only be held in memory until the application is closed, the data will be available any time that this activity starts within the current lifetime of the application. • To store data between application instances (ie permanently) use SharedPreferences • This data is written to the database on the device and is available all the time.
Storing data • Via OnsaveInstanceState and a bundle • A bundle has a set of get/put methods for strings, ints, etc.. • Using a key and data public void onSaveInstanceState(Bundle savedInstanceState) { String d1 = t1.getText().toString(); savedInstanceState.putString("d1", d1); super.onSaveInstanceState(savedInstanceState); }
Restoring data. • Via OnCreate(Bundle) if (savedInstanceState != null) { //There is saved data d1 = savedInstanceState.getString("d1"); if (d1 != null) { //and d1 key exists t1.setText(d1); } }
SharedPreferences • Generally you want to store Preferences in OnPause() • You restore preferences in OnCreate() and/or onPause() • Remember onPause() is called after OnCreate() • So it may not need to restore preferences in OnCreate() • unless you use the data in OnCreate
SharePreferences • Also, you can set it up to share between activities or per activity. • The difference is the how you get the preferences • Activity preferences: • SharedPreferences preferences = getPreferences(MODE_PRIVATE); • Application preferences • SharedPreferences preferences = getSharedPreferences(“example",MODE_PRIVATE); • Both are private, but there is deprecated MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, MODE_MULTI_PROCESS
Storing • In OnPause() Super.onPause(); //must be called • Get an spot to start the preferences and edit them. SharedPreferencespreferences = getPreferences(MODE_PRIVATE); • OR SharedPreferences preferences = getSharedPreferences(“example",MODE_PRIVATE); • Now set it to editable. SharedPreferences.Editoreditor = preferences.edit(); • Use the put methods with a key value and then commit them to memory editor.putString("d3",d3); editor.commit();
Restoring Preferences • Since, I’m restoring in both onCreate() and OnResume() • I’ll use a separate method, called getprefs(); • Note in OnResume(), you must call super.OnResume()
getPrefs() void getprefs() { • Get the between instance stored values SharedPreferencespreferences = getPreferences(MODE_PRIVATE); • OR SharedPreferences preferences = getSharedPreferences(“example",MODE_PRIVATE); • Now read them back. d3 = preferences.getString("d3", ""); • Like preferenceActivity, we set a default value for the key • In this case “”, if the d3 key doesn’t exist. t3.setText(d3); }
References • http://developer.android.com/reference/android/app/Activity.html • http://www.eigo.co.uk/Managing-State-in-an-Android-Activity.aspx • http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state
Screen Orientation. • As a note, remember you can set the screen orientation in via the manifest.xml file. • In the <activity> tag • Put android:screenOrientation="portrait“ • Or “landscape”
Q A &