Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 – app/Activity.html app/Activity.html

5 SavedInstance vs SharePreferences 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 Preferences 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 Storing In OnPause() Super.onPause(); //must be called Get an spot to start the preferences and edit them. SharedPreferences preferences = getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); Use the put methods with a key value and then commit them to memory editor.putString("d3",d3); editor.commit();

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

11 getPrefs() void getprefs() { Get the between instance stored values SharedPreferences preferences = getPreferences(MODE_PRIVATE); 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); }

12 Shared Preferences The previous preferences can only be used in the activity that created them. SharedPreferences can be used between activities in the same package (even if private).

13 Shared Preferences (2) Works just like previous preferences, with a change to how it’s opened. – SharedPreferences preferences = getSharedPreferences("example", Context.MODE_PRIVATE); Where Example is the name of your preferences. Otherewise, no other changes are needed in reading or “writing” the data.

14 References oid/app/Activity.html Android-Activity.aspx ow-do-i-save-an-android-applications-state

15 Screen Orientation. As a note, remember you can set the screen orientation in via the manifest.xml file. In the tag Put android:screenOrientation="portrait“ – Or “landscape”

16 Q A &


Download ppt "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."

Similar presentations


Ads by Google