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.

Slides:



Advertisements
Similar presentations
Programming with Android: Activities
Advertisements

ANDROID DEVELOPMENT KELLY MCBEAN. DEVELOPMENT ENVIRONMENT OVERVIEW Eclipse Standard IDE for Developing Android Applications Install: 1.Java (JDK) – Since.
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
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.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
Android Fragments.
More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View.
Creating Android user interfaces using layouts 1Android user interfaces using layouts.
CS378 - Mobile Computing Anatomy of an Android App and the App Lifecycle.
Android: versions Note that: Honeycomb (Android v3.0) A tablet-only release Jelly Bean (Android v4.1) Released on July 09, 2012.
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.
Android Mobile computing for the rest of us.* *Prepare to be sued by Apple.
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
Activities and Intents. Activities Activity is a window that contains the user interface of your application,typically an application has one or more.
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.
Android – Fragments L. Grewe.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
Noname. Conceptual Parts States of Activities Active Pause Stop Inactive.
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
Android: “Dynamic” data and Preferences data.
Android Application Lifecycle and Menus
Build Cycle 5.  Themes  US Presidents  Custom Images  Number of Moves Made  Posting to Facebook.
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
Android Fragments. Slide 2 Lecture Overview Getting resources and configuration information Conceptualizing the Back Stack Introduction to fragments.
Editing a Twitter search. Viewing search results in a browser.
Open Handset Alliance.
Mobile Application Development BSCS-7 Lecture # 2
SQLite in Android Landon Cox March 2, 2017.
Activity and Fragment.
Adapting to Display Orientation
Basic Activities and Intents
Activities, Fragments, and Events
Fragment ?.
Fragments: Introduction
CS499 – Mobile Application Development
Mobile Application Development BSCS-7 Lecture # 6
Activities and Intents
Android 9: The Activity Lifecycle
Anatomy of an Android App and the App Lifecycle
Widgets & Fragments Kalin Kadiev Astea Solutions AD.
Android Mobile Application Development
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
The Android Activity Lifecycle
Android – Fragments L. Grewe.
ANDROID UI – FRAGMENTS UNIT II.
Android training in Chandigarh. What is ADB ADB stands for Android Debug Bridge. It is a command line tool that is used to communicate with the emulator.
Anatomy of an Android App and the App Lifecycle
Activity Lifecycle Fall 2012 CS2302: Programming Principles.
Adding Functionality to the App Tip Calculator App Activity and the Activity Lifecycle onCreate is called by the system when an Activity.
Android Topics Android Activity Lifecycle and Experiment Toast
Activities and Intents
HNDIT2417 Mobile Application Development
Programming Mobile Applications with Android
Activity Lifecycle.
Activities and Intents
Activities and Intents
SE4S701 Mobile Application Development
Activities and Fragments
Android Development Tools
Implication of Orientation Changes
Preference Activity class
Korea Software HRD Center
Activities, Fragments, and Intents
Presentation transcript:

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 Activity class Activity's event handlers onCreate(): when first created onStart(): when visible onResume(): when interactive onPause(): when paused onStop(): when no longer visible onDestroy(): prior to destruction onSaveInstanceState(Bundle)

Activity States The state of an activity depends on Activity states Its position in the Activity stack Activity states Active Activity at the top of the stack Paused Activity does not have focus Stopped Activity is not visible Inactive After an activity has been killed

Event Handlers to Monitor State Changes Event handlers are defined to Enable activities to react to state changes Full lifetime: Between onCreate and onDestroy Visible lifetime: Bound between onStart and onStop Active lifetime Starts with onResume and ends with onPause

Applying Themes to an Activity By default, An activity occupies the entire screen However, One can apply a dialog theme to an activity This way, it displays as a floating dialog How?

Applying Themes to an Activity (Cont’d) One can hide the title of an activity By applying the following theme to your application: @android:style/Theme.NoTitleBar Refer to ActivityAsDialog Android project

Techniques for Handling Orientation Changes 2 techniques are available Anchoring Anchor views to edges of the screen Can be done by means of RelativeLayouts Resizing and repositioning Resize every view as a function of the current orientation Refer to AlternativeLayouts Android project

Anchoring views RelativeLayout On orientation change Does the trick Buttons remain Anchored to screen edges

Output

Resizing and Positioning Idea Create a different layout for each mode By creating a new subfolder under res Named layout-land This way, there will be main.xml contained in layout Defining UI for portrait mode main.xml in layout-land Handling UI in landscape mode

Example: layout

Example: layout-land

Output

Detecting Orientation Changes Device’s current orientation Can be detected during runtime as follows:

Controlling Orientation of An Activity A change in orientation Can be forced programmatically as follows

Alternative Method for Controlling Orientation Alternatively, orientation can be forced as follows

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 Fixes 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);