Preserving Activity State in Android Development

Slide Note
Embed
Share

In Android app development, changing the screen orientation can destroy and recreate the activity, potentially leading to loss of the current state. To tackle this issue, various fixes exist such as implementing the onSaveInstanceState() method and utilizing SharedPreferences class to preserve and restore data. Other methods involve using files stored in folders for persistent data storage and implementing AlertDialogs for user interactions.


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.



Uploaded on Apr 06, 2024 | 2 Views


Presentation Transcript


  1. Implication of Orientation Changes Problem Changing screen orientation destroys activity and recreates it On recreation, current state of activity could be lost => Need to preserve the state of an activity Many fixes including Implement the onSaveInstance() method Use SharedPreferences class Refer to PreservingStateApp Android project

  2. Fix#1: onSaveInstanceState Idea Preserve state and restore it later Use the Bundle object to save current state: Upon recreation, retrieve state saved previously: Limitation Not adequate for recovering complex data structures

  3. Fix#2: Using SharedPreferences SharedPrefernces Points to a file containing key-value pairs Providing simple methods to read and write them By: Getting a reference to a SharedPreference: Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences( getString(R.string.preference_file_key), Context.MODE_PRIVATE); then, writing to a shared preference: SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.saved_high_score), newHighScore); editor.commit(); Then finally, reading data from shared perference SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); int defaultValue = getResources().getInteger(R.string.saved_high_score_default); long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

  4. Other Fixes Using files that can be placed in files folder Data stored persistently in Folder can be obtained through: File getFilesDir() Or cache folder Data cleared if resources is needed somewhere else Folder can be obtained via: File getCacheDir() Using channels and buffers

  5. AlertDialog A dialog that Can show a title, up to three buttons, and a list of selectable items Creating a dialog fragment public class FireMissilesDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.dialog_fire_missiles) .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // FIRE ZE MISSILES! } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); // Create the AlertDialog object and return it return builder.create(); } }

Related