Android Topics 1 Android Activity Lifecycle and Experiment


















- Slides: 18

Android Topics 1. Android Activity Lifecycle and Experiment 2. Toast 3. Explore Online Android Documentation 4. Practice: In-Class App

Android Activity • Every application has at least one activity class. • A main activity is presented to the user when the application is launched for the first time. Example: Users of a banking app must first log into their accounts before they can pay bills or transfer money. Login is the main activity. • Each activity can start another activity in order to perform different actions. • Each time a new activity starts, the previous activity is paused and the Android system preserves its status.

Android Activity and the Android. Manifest. xml • Activities must be declared in the Android. Manifest. xml file in order to be accessible to the system. • The category of the main activity is specified as the top-levellauncher.

Android. Manifest. xml <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="com. example. trishcornez. mediaplayer"> <application android: allow. Backup="true" android: icon="@mipmap/ic_launcher" android: label="@string/app_name" android: supports. Rtl="true" android: theme="@style/App. Theme"> <activity android: name=". Main. Activity"> <intent-filter> <action android: name="android. intent. action. MAIN" /> <category android: name="android. intent. category. LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

Activity Class callback methods 1. on. Create() 2. on. Start() 3. on. Resume() 4. on. Pause() 5. on. Stop() 6. on. Restart() 7. on. Destroy() A callback method gets called when an event occurs.


Toast: Development feature Example A Toast can be programmed to appear when an event occurs, such as when the user clicks a button or when an activity is created. A Toast appears and then vanishes.

Experiment : Lab 3 Exercise 1

Objective of Exploring Online Documentation • Navigate documentation • Research constructs • Build apps that we have very little clue about at the start.

Development Tips • Never build an entire app and then test it. • Build and test in increments. • Use development tools, such as Android logs and Toast to aid your process. Android Logs: Following certain events, include a message to the Android logs. For the Log tag, use the name of activity class. For the Log message, use a keyword name and a value. Toast: Can be used to display a temporary message box to the screen.

In-Class App: Sound Player • The app is loaded with a sound (Mad Cow) • Sound can be played. • Sound can be paused and then played again, continuing at the pause point.

Where to begin? What do we know? 1. Sound is a media element. 2. This app requires Media. Player for Android. 3. Media. Player will need to be researched. 4. Sound is stored as raw data. 5. Create a raw directory in the resources directory and load it with an interesting sound.

Controller View Model

Overview of Development Tasks • Task 1: Build a Layout for the app. • Task 2: Test the app using Toast. Quick experiment. • Task 3: Research Media. Player for sound. • Task 4: Load the app with an interesting sound. • Task 5: Implement the Media. Player. • Task 6: Test the app: play the sound, pause it, and continue playing it. • Task 7: Remove Toasts when done. No Stop action is added to this app. It requires a little more finesse.

• Task 1: Build a Layout for the app. • Task 2: Test the button using Toast. Quick experiment.

• Task 3: Research Media. Player for sound. a. b. c. d. e. f. g. What is Media. Player? Is it a class or an object? What does the Media. Player state diagram tells us about playback controls? How do we play a sound or pause a sound? How do we start the playback of a sound. How do we resume playback for a paused Media. Player object? What is required to to play a specific sound? How do we identify a raw file?

• Task 4: Load the app with an interesting sound.

• Task 5: Implement the Media. Player. • Task 6: Test the app: play the sound, pause it, and continue playing it. • Task 7: Remove Toasts when done.