EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Similar documents
EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

ITU- FAO- DOA- TRCSL. Training on. Innovation & Application Development for E- Agriculture. Shared Preferences

Create Parent Activity and pass its information to Child Activity using Intents.

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

EMBEDDED SYSTEMS PROGRAMMING Android Services

EMBEDDED SYSTEMS PROGRAMMING Android NDK

Vienos veiklos būsena. Theory


Tip Calculator. xmlns:tools=" android:layout_width="match_parent"

Intents. Your first app assignment

Arrays of Buttons. Inside Android

MAD ASSIGNMENT NO 3. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Basic GUI elements - exercises

StoppUhr. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start1"

ANDROID PROGRAMS DAY 3

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

EMBEDDED SYSTEMS PROGRAMMING Android NDK

Fragment Example Create the following files and test the application on emulator or device.

android-espresso #androidespresso

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

Android Application Development

Data Persistence. Chapter 10

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

Fragments. Lecture 11

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

M.A.D ASSIGNMENT # 2 REHAN ASGHAR BSSE 15126

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

ELET4133: Embedded Systems. Topic 15 Sensors

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Android Data Storage

PROGRAMMING APPLICATIONS DECLARATIVE GUIS

Android Programs Day 5

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

TextView Control. EditText Control. TextView Attributes. android:id - This is the ID which uniquely identifies the control.

Applied Cognitive Computing Fall 2016 Android Application + IBM Bluemix (Cloudant NoSQL DB)

M.A.D Assignment # 1

Starting Another Activity Preferences

PENGEMBANGAN APLIKASI PERANGKAT BERGERAK (MOBILE)

Mobile Software Development for Android - I397

Software Practice 3 Before we start Today s lecture Today s Task Team organization

Saving application preferences

Mobila applikationer och trådlösa nät, HI1033, HT2012

Solving an Android Threading Problem

Android Basics. Android UI Architecture. Android UI 1

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

CMSC436: Fall 2013 Week 3 Lab

CSE 660 Lab 7. Submitted by: Arumugam Thendramil Pavai. 1)Simple Remote Calculator. Server is created using ServerSocket class of java. Server.

Computer Science E-76 Building Mobile Applications

Mobile Computing Practice # 2c Android Applications - Interface

Have a development environment in 256 or 255 Be familiar with the application lifecycle

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

Manifest.xml. Activity.java

Embedded Systems Programming - PA8001

Android Beginners Workshop

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

android:orientation="horizontal" android:layout_margintop="30dp"> <Button android:text="button2"

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

<uses-permission android:name="android.permission.internet"/>

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Android HelloWorld - Example. Tushar B. Kute,

Application s Life Cycle

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar


Create a local SQL database hosting a CUSTOMER table. Each customer includes [id, name, phone]. Do the work inside Threads and Asynctasks.

Application s Life Cycle

Hello World. Lesson 1. Android Developer Fundamentals. Android Developer Fundamentals. Layouts, and. NonCommercial

Topics of Discussion

SD Module-1 Android Dvelopment

1. Location Services. 1.1 GPS Location. 1. Create the Android application with the following attributes. Application Name: MyLocation

CS 528 Mobile and Ubiquitous Computing Lecture 3: Android UI, WebView, Android Activity Lifecycle Emmanuel Agu

Produced by. Mobile Application Development. Eamonn de Leastar

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Dynamically Create Admob Banner and Interstitial Ads

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

App Development for Smart Devices. Lec #7: Audio, Video & Telephony Try It Out

Overview. What are layouts Creating and using layouts Common layouts and examples Layout parameters Types of views Event listeners

Mobila applikationer och trådlösa nät, HI1033, HT2013

Simple Currency Converter

Our First Android Application

Chapter 8 Positioning with Layouts

Mobile Computing Fragments

CS 193A. Activity state and preferences

Android UI Development

IPN-ESCOM Application Development for Mobile Devices. Extraordinary. A Web service, invoking the SOAP protocol, in an Android application.

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

UI (User Interface) overview Supporting Multiple Screens Touch events and listeners

else if(rb2.ischecked()) {

By Ryan Hodson. Foreword by Daniel Jebaraj

User Interface Development. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr.

Upon completion of the second part of the lab the students will have:

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

Applications. Marco Ronchetti Università degli Studi di Trento

ANDROID USER INTERFACE

Developed and taught by well-known Contact author and developer. At public for details venues or onsite at your location.

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

Transcription:

EMBEDDED SYSTEMS PROGRAMMING 2016-17 Application Tip: Saving State

THE PROBLEM How to save the state (of a UI, for instance) so that it survives even when the application is closed/killed The state should be restored when the application is opened again: to the user it is as if the application has always been there We will consider a UI with an editable text field, a checkbox, and a seekbar Solution: use the facilities provided by (and follow the rules dictated by) Android s frameworks

ACTIVITIES AND APPS Android UIs are managed inside activities An Android app may contain several activities Activities are independent by a large extent, but they share some resources associated with the app (e.g., the user ID) An activity may be destroyed by the OS while the app that hosts it survives. Later on, a new instance of the same activity may be created

INSTANCE STATE If an activity is destroyed and recreated by the OS (e.g., because the screen is rotated): the new activity is a new instance of the old one If an activity is destroyed due to normal app behavior (user presses the Back button, destroy() is invoked): the activity instance is gone forever Instance state: information associated with an activity instance

PERSISTENT STATE Persistent state: information associated with an application (e.g., user preferences) Persistent state must be preserved between different runs of an application. Instance state must not

THE TIP: INSTANCE STATE (1/2) Consider the HelloWithButton example When the screen orientation changes, the HelloWithButton activity is destroyed, then a new instance of the activity is launched to redraw the screen according to the new orientation Let s modify the HelloWithButton example so that the instance state is preserved

THE TIP: INSTANCE STATE (2/2) The state of the TextView must be saved Platform solution: save it in the savedinstancestate Bundle

CODE (1/3) Only one source file: HelloWithButton.java package it.unipd.dei.esp1516.saveinstancestate; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.linearlayout; import android.widget.textview; public class HelloWithButton extends AppCompatActivity { /* Class variables */ private TextView tv; private Button bu; /** Called when the activity is first created. */ @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // Create the TextView tv = new TextView(this); tv.settext("press the button, please");

CODE (2/3) // Restore TextView state from the savedinstancestate if (savedinstancestate!= null) { String strvalue = savedinstancestate.getstring("strtv"); if (strvalue!= null) tv.settext(strvalue); } } // Create the Button bu = new Button(this); bu.settext("press me"); // Set the action to be performed when the button is pressed bu.setonclicklistener(new View.OnClickListener() { public void onclick(view v) { // Perform action on click tv.settext("good job!"); } }); // Create the layout LinearLayout mylayout = new LinearLayout(this); // Add the UI elements to the layout mylayout.addview(bu); mylayout.addview(tv); // Display the layout setcontentview(mylayout);

CODE (3/3) /** Called when the system is about to pause the activity because it is * resuming a previous one. This method allows you to save any * dynamic INSTANCE state in your activity into the given Bundle, * to be later received in oncreate(bundle) if the activity needs * to be re-created. * Note: PERSISTENT state (which is different from instance state!) * should be saved in the onpause() method because onsaveinstancestate() * is not part of the life cycle callbacks, hence it will not be called * in every situation */ @Override public void onsaveinstancestate(bundle savedinstancestate) { // NOTE: with the implementation of this method inherited from // Activity, some widgets save their state in the bundle by default. // Once the user interface contains AT LEAST one non-autosaving // element, you should provide a custom implementation of // the method String strtv = tv.gettext().tostring(); savedinstancestate.putstring("strtv", strtv); } } super.onsaveinstancestate(savedinstancestate);

THE TIP: PERSISTENT STATE (1/2) Back on track: the UI state must now survive even when the application is closed/killed (this is not the case for HelloWithButton: try force-quitting it) Platform solution: save data as SharedPreferences

THE TIP: PERSISTENT STATE (2/2) New application: the UI contains an editable text field, a checkbox, and a seekbar

CODE (1/6) Source files: PersistenceActivity.java Other resources: activity_persistence.xml (UI layout), strings.xml (UI strings)

CODE (2/6) strings.xml <resources> <string name="app_name">savepersistentstate</string> <string name="hello">please enter some data below</string> <string name="please1">please enter your name</string> <string name="please2">check the box if you like</string> <string name="please3">slide the cursor a bit</string> <string name="label">checkbox</string> </resources>

CODE (3/6) activity_persistence.xml (1/2) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="it.unipd.dei.esp1516.savepersistentstate.persistenceactivity"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:minheight="32dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearlayout1"> <TextView android:text="@string/please1" android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minwidth="130dp"/> <EditText android:text="" android:id="@+id/edittext1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:inputtype="text"/> </LinearLayout>

CODE (4/6) activity_persistence.xml (2/2) <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearlayout2"> <TextView android:text="@string/please2" android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content android:minwidth="130dp"/> <CheckBox android:text="@string/label" android:id="@+id/checkbox1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearlayout3"> <TextView android:text="@string/please3" android:id="@+id/textview3 android:layout_width="wrap_content android:layout_height="wrap_content android:minwidth="130dp"/> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content android:id="@+id/seekbar1"/> </LinearLayout> </LinearLayout>

CODE (5/6) PersistenceActivity.java (1/2) package it.unipd.dei.esp1516.savepersistentstate; import android.annotation.suppresslint; import android.content.sharedpreferences; public class PersistenceActivity extends AppCompatActivity { /** Called when the activity is first created. */ @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // Set the view setcontentview(r.layout.activity_persistence); // Get persistent data stored as SharedPreferences SharedPreferences preferences = getpreferences(mode_private); String str_et = preferences.getstring("edittextvalue", null); boolean bln_cb = preferences.getboolean("checkboxvalue", false); int int_sb = preferences.getint("seekbarvalue", 0); } // Get references to widgets and set them according to persistent data EditText et = (EditText)findViewById(R.id.editText1); et.settext(str_et); CheckBox cb = (CheckBox)findViewById(R.id.checkBox1); cb.setchecked(bln_cb); SeekBar sb = (SeekBar)findViewById(R.id.seekBar1); sb.setprogress(int_sb);

CODE (6/6) PersistenceActivity.java (2/2) /** Called as part of the activity lifecycle when an activity is going * into the background, but has not (yet) been killed. * The counterpart to onresume(). */ @SuppressLint("CommitPrefEdits") @Override protected void onpause() { super.onpause(); } } // Store values between instances here SharedPreferences preferences = getpreferences(mode_private); SharedPreferences.Editor editor = preferences.edit(); // Get references to widgets and read the status // of them all EditText et = (EditText)findViewById(R.id.editText1); String str_et = et.gettext().tostring(); CheckBox cb = (CheckBox)findViewById(R.id.checkBox1); boolean bln_cb = cb.ischecked(); SeekBar sb = (SeekBar)findViewById(R.id.seekBar1); int int_sb = sb.getprogress(); // Store status in the preferences editor.putstring("edittextvalue", str_et); editor.putboolean("checkboxvalue", bln_cb); editor.putint("seekbarvalue", int_sb); // Commit to storage synchronously editor.commit();

LAST MODIFIED: MARCH 26, 2017 COPYRIGHT HOLDER: CARLO FANTOZZI (FANTOZZI@DEI.UNIPD.IT) LICENSE: CREATIVE COMMONS ATTRIBUTION SHARE-ALIKE 3.0