Implication of Orientation Changes

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Advertisements

CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
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.
Programming with Android: Activities
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
User Interface Classes.  Design Principles  Views & Layouts  Event Handling  Menus  Dialogs.
Cosc 4730 Android Dialogs and notifications. Notifications There are a couple of ways to notify users without interrupting what they are doing The first.
Data Storage: Part 1 (Preferences)
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Mobile Computing Lecture#11 Adapters and Dialogs.
Android Storage. There are several options for storage of data with Android We can put data into a preferences file. We can put data into a ‘normal’ file.
Nilesh Singh Local Data Storage option Android provides several options for you to save persistent application data. - Shared preferences - Creation.
Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.
Android – Fragments L. Grewe.
Cosc 5/4730 Dialogs and below 3.0 and above (fragment)
Data Persistence Nasrullah Niazi. Share Preferences The SharedPreferences class provides a general framework that allows you to save and retrieve persistent.
User notification Android Club Agenda Toast Custom Toast Notification Dialog.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Styles, Dialog Boxes, and Menus. Styles Allow creation of a common format – placed in res/values/styles.xml – file name is incidental Can be applied.
Mobile Programming Lecture 7 Dialogs, Menus, and SharedPreferences.
Android: “Dynamic” data and Preferences data.
Android Application Lifecycle and Menus
CS378 - Mobile Computing Audio.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Android Drawing Units and The.
The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Android Alert Dialog. Alert Dialog Place Button to open the dialog. public class MainActivity extends ActionBarActivity { private static Button button_sbm;
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
Mobile Application Development Data Storage. Android provides several options for you to save persistent application data. The solution you choose depends.
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
The Flag Quiz app tests your ability to correctly identify 10 flags from various countries and territories.
Editing a Twitter search. Viewing search results in a browser.
ANDROID DIALOGS. Slide 2 Dialogs (Introduction) The Dialog class is the base class for all dialogs A dialog is a small window that prompts the user to.
Windows 7 Ultimate To Load Click Simulation PPSX
Android Application Audio 1.
Mobile Applications (Android Programming)
CS499 – Mobile Application Development
CS371m - Mobile Computing Persistence.
Android Application Data Storage 1.
SQLite in Android Landon Cox March 2, 2017.
GUI Programming Fundamentals
Further android gui programming
Android 13: Dialogs and Notifications
Android 4: Saving Data Kirk Scott.
Android Activities An application can have one or more activities, where Each activity Represents a screen that an app present to its user Extends the.
android architecture components with mvvm
Android 18: Saving Data Kirk Scott.
ANDROID UI – FRAGMENTS UNIT II.
Mobile Computing With Android ACST 4550 Alerts
Chapter 3: Coding the GUI Programmatically, Layout Managers
Android Storage.
Lesson 9 Dialog Boxes & Toast Widgets Victor Matos
Adding Functionality to the App Tip Calculator App Activity and the Activity Lifecycle onCreate is called by the system when an Activity.
Android: Preferences and user settings data
Favorite Twitter Searches App
Android Developer Fundamentals V2
Mobile Computing With Android ACST 4550 Android Database Storage
Activities and Intents
Activities and Intents
Favorite Twitter Searches App
Android Developer Fundamentals V2 Lesson 4
Notifying from the Background
Preference Activity class
Activities, Fragments, and Intents
External Services CSE 5236: Mobile Application Development
External Services CSE 5236: Mobile Application Development
Presentation transcript:

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

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

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: then, writing to a shared preference: Then finally, reading data from shared perference Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences(         getString(R.string.preference_file_key), Context.MODE_PRIVATE); SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.saved_high_score), newHighScore); editor.commit(); 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);

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

AlertDialog A dialog that Creating a dialog fragment 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();     } }