1 / 15

Cosc 5/4730

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

sadah
Download Presentation

Cosc 5/4730

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cosc 5/4730 Android: “Dynamic” data.

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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); }

  7. 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); } }

  8. 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

  9. 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

  10. 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();

  11. 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()

  12. 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); }

  13. 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

  14. 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”

  15. Q A &

More Related