Objects First with Java

Slides:



Advertisements
Similar presentations
CE881: Mobile and Social Application Programming Simon M. Lucas Quiz, Walkthrough, Exercise, Lifecycles, Intents.
Advertisements

CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
Programming with Android: Activities
Android 02: Activities David Meredith
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
Manifest File, Intents, and Multiple Activities. Manifest File.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View.
CS378 - Mobile Computing Anatomy of an Android App and the App Lifecycle.
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
CS5103 Software Engineering Lecture 08 Android Development II.
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.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
@2011 Mihail L. Sichitiu1 Android Introduction GUI Menu Many thanks to Jun Bum Lim for his help with this tutorial.
Copyright© Jeffrey Jongko, Ateneo de Manila University Of Activities, Intents and Applications.
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Android – Fragments L. Grewe.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Linking Activities using Intents How to navigate between Android Activities 1Linking Activities using Intents.
Noname. Conceptual Parts States of Activities Active Pause Stop Inactive.
Activity Android Club Agenda Hello Android application Application components Activity StartActivity.
Mobile Programming Midterm Review
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.
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to.
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
Android Application Lifecycle and Menus
Intents 1 CS440. Intents  Message passing mechanism  Most common uses:  starting an Activity (open an , contact, etc.)  starting an Activity.
Android Intents Nasrullah. Using the application context You use the application context to access settings and resources shared across multiple activity.
Lecture 2: Android Concepts
Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück.
Lecture 8 the Preference Menu. Settings... Sudoku settings s Music Play background music Hints Show hints during play We add some strings to the strings.xml.
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
Menus. Menus are a common user interface component in many types of applications. The options menu is the primary collection of menu items for an activity.
Lab7 – Appendix.
Introduction to android
Intents and Broadcast Receivers
Activity and Fragment.
Programming with Android:
Linking Activities using Intents
Basic Activities and Intents
Activities, Fragments, and Events
Fragment ?.
Mobile Application Development BSCS-7 Lecture # 6
Activities and Intents
Anatomy of an Android App and the App Lifecycle
Android Mobile Application Development
The Android Activity Lifecycle
ANDROID UI – FRAGMENTS UNIT II.
CS5103 Software Engineering
Anatomy of an Android App and the App Lifecycle
Activity Lifecycle Fall 2012 CS2302: Programming Principles.
Activities and Intents
Activities and Intents
HNDIT2417 Mobile Application Development
Programming Mobile Applications with Android
Activity Lifecycle.
Activities and Intents
Activities and Intents
SE4S701 Mobile Application Development
Objects First with Java
Activities and Fragments
Android Development Tools
Activities, Fragments, and Intents
Presentation transcript:

Objects First with Java Class Business © David J. Barnes and Michael Kölling

User Interface The UI for an Android app is built using a hierarchy of View  and ViewGroup objects.  View objects are usually UI widgets such as buttons or textfields  ViewGroup objects are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list.

Assignment?

Activity An Activity is the main object in an Android app. Lifecycle:

Activity Created Redefine onCreate() Good time to Define the interface with setContentView() Initialize any parts of the interface, including callback assignments Start any necessary threads/services (more on this later)

GradeBox Example

Activity Started Created activities are also started Redefine onStart() Good time to Read preferences Probe resources

Resume an Activity This means the activity has been pulled to the foreground and the user can interact with it. New activities are also resumed. Define onResume() to do something if necessary Good time to Check any indicators (e.g. email) and refresh user interface items Obtain system resources (e.g. camera)

GradeBox Example

Activity Paused Redefine onPause() Activity is obscured Good time to Reset user interface indicators Pause any realtime actions Release system resources (e.g. camera)

Activity Stopped Redefine onStop() Activity is completely hidden and considered to be in the background onPause() is called first Good time to act as if process terminated Write user data Write preferences Act like the app is shut down

GradeBox Example

Activity Restarted Redefine onRestart() Activity is resurrected Good time to act as if process were created Refresh user data and preferences Refresh the interface

Activity Destroyed Redefine onDestroy() Activity is completely removed from the system Good time to Reset system components

Example: Rotation Rotation causes a number of events to take place: App is stopped: pause, then stop (save state) App is restarted: restart, then resumed (reinstate state)

Dialogs To display dialog boxes, do two things: Define onCreateDialog() and pay attention to the parameter. Create a dialog box and return it. Call showDialog() with an integer to indicate which dialog you want.

GradeBox Example

Menus Override the onCreateOptions() call Example: public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.opening_menu, menu); return true; }

Menus Define your menu in XML spec file <menu> <item android:id="@+id/opening_menu_new_item" android:icon="@drawable/ic_menu_sun" android:title="New" /> <item android:id="@+id/opening_menu_import_item" android:icon="@drawable/ic_menu_filter" android:title="Import"> </item>

Response to Menu Events Redefine onOptionsItemSelected() public boolean onOptionsItemSelected(MenuItem item){ switch (item.getItemId()) { case R.id.item1: return true; case R.id.item2: startActivity(new Intent(getApplicationContext(), ActivityTwo.class)); default: return super.onContextItemSelected(item); 12: } 13: }

GradeBox Example

Other Programmable Events Examples onPrepareDialog() before onCreateDialog() onCreateContextMenu() for popup menus onContextItemSelected() for popup menu items selected onCreateOptionsMenu() to create menus onOptionsItemSelected() from other menus Etc…etc…etc…

Machine Problem #4

Starting New Activities A common action for an activity is to start another activity. When another activity is started, the current activity is stopped and the new activity is created/started/resumed.

Starting New Activities: Intents Intents are communications about classes or executables or data Example: communicate which activity to start by creating an intent that indicates the class to use

Starting New Activities Two ways to start an activity To start an independent activity that will run, use startActivity() To start an activity that will return information to the current activity, use startActivityForResult() Both calls use intents to indicate which activity should start up

startActivity() Uri smsUri = Uri.parse("tel:"+grades.getStudent(itemPosition). getMobilePhoneNumber()); Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsUri); sendIntent.putExtra("address", grades.getStudent(itemPosition).getMobilePhoneNumber()); sendIntent.putExtra("sms_body", "type message here"); sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent);

startActivityForResult() Using this call, one gives the intent AND a returning value that identifies the activity. When the Activity is completed, the method onActivityResult() is called.

startActivityForResult() Intent gameIntent = new Intent(this, GameDisplay.class); startActivityForResult( gameIntent, R.id.class_display_games);

startActivityForResult() protected void onActivityResult( int requestCode, int resultCode, Intent intent) { super.onActivityResult( requestCode, resultCode, intent); if (resultCode != RESULT_OK) return; if (intent != null) { if (requestCode == R.id.class_display_games) { … }

GradeBox Example

Returning From an Activity To halt an Activity, call finish() To return to the Activity that created the current Activity, call setResult(): setResult(RESULT_OK, result); This means automatic return with the ID that it was created with.

GradeBox Example

Information in Intents You can insert information into an intent to communicate with the activity that is being started. The typical way is to Create a Bundle Put things into the Bundle Add the Bundle to the Intent through putExtras()

GradeBox Example

Retrieving Information Use the getIntent() call to retrieve the Intent Use the get…() call to get the extra you are looking for

GradeBox Example

Better Ways to Organize the Code There are several times when the code is organized around an integer return value. onCreateDialog() onOptionsItemSelected() onActivityResult() This results in a string of “if…then…else” code that just stinks.

Better Ways to Organize the Code Use a table of classes. Do a table lookup of the code and Java reflection to call the right class.

GradeBox Example

Files Files are done "normally" from the Java perspective The location of files is not normal Because of security, "file space" is buried deep in the Android file system.

Open local file FileInputStream classFile = openFileInput("ClassLocations"); … classFile.close(); FileOutputStream file = openFileOutput("ClassLocations"); file.close();

Open Local File Do not open an absolute path name unless you know where your app is stored. Can get the path as String f = getFilesDir().getAbsolutePath();

Open online file DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httppost = new HttpGet("http://www.urlOfThePageYouWantToRead.nl/text.txt"); HttpResponse response = httpclient.execute(httppost); HttpEntity ht = response.getEntity(); BufferedHttpEntity buf = new BufferedHttpEntity(ht); InputStream is = buf.getContent(); BufferedReader r = new BufferedReader(new InputStreamReader(is));