CIS 694EEC 693 Android Sensor Programming Lecture 3

  • Slides: 22
Download presentation
CIS 694/EEC 693 Android Sensor Programming Lecture 3 Wenbing Zhao Department of Electrical Engineering

CIS 694/EEC 693 Android Sensor Programming Lecture 3 Wenbing Zhao Department of Electrical Engineering and Computer Science Cleveland State University w. zhao 1@csuohio. edu 11/10/2020 CIS 470: Mobile App Development 1

Activities, Fragments, and Intents n n The life cycles of an activity Using fragments

Activities, Fragments, and Intents n n The life cycles of an activity Using fragments to customize your UI Understanding the concept of intents Displaying alerts to the user using notifications 11/10/2020 CIS 470: Mobile App Development 2

Activities n n n Typically, applications have one or more activities The main purpose

Activities n n n Typically, applications have one or more activities The main purpose of an activity is to interact with the user An activity’s life cycle: from the moment an activity appears on the screen to the moment it is hidden, it goes through a number of stages 11/10/2020 CIS 470: Mobile App Development 3

Fragments n n n Fragment is a feature that was introduced for tablets in

Fragments n n n Fragment is a feature that was introduced for tablets in Android 3. 0 and for phones in Android 4. 0 Think of fragments as “miniature” activities that can be grouped to form an activity 11/10/2020 CIS 470: Mobile App Development 4

Intent n An intent is the “glue” that enables activities from different applications to

Intent n An intent is the “glue” that enables activities from different applications to work together seamlessly, ensuring that tasks can be performed as though they all belong to one single application 11/10/2020 CIS 470: Mobile App Development 5

Activity Life Cycle 11/10/2020 CIS 470: Mobile App Development 6

Activity Life Cycle 11/10/2020 CIS 470: Mobile App Development 6

Activity Life Cycle n n n n on. Create()—Called when the activity is first

Activity Life Cycle n n n n on. Create()—Called when the activity is first created on. Start()—Called when the activity becomes visible to the user on. Resume()—Called when the activity starts interacting with the user on. Pause()—Called when the current activity is being paused and the previous activity is being resumed on. Stop()—Called when the activity is no longer visible to the user on. Destroy()—Called before the activity is destroyed by the system (either manually or by the system to conserve memory) on. Restart()—Called when the activity has been stopped and is restarting again 11/10/2020 CIS 470: Mobile App Development 7

Observe Activity Life Cycle Using Android n n Studio, create a new Android project

Observe Activity Life Cycle Using Android n n Studio, create a new Android project and name it Activity 101 In the Activity 101 Activity. ja va file, add the following highlighted statements q Throughout this example, be sure to change all references to "com. jfdimarzio" to whatever package name your project is using 11/10/2020 package com. jfdimarzio. activity 101; import android. support. v 7. app. App. Compat. Activity; import android. os. Bundle; import android. util. Log; public class Main. Activity extends App. Compat. Activity { String tag = "Lifecycle Step"; @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_main); Log. d(tag, "In the on. Create() event"); } public void on. Start() { super. on. Start(); Log. d(tag, "In the on. Start() event"); } public void on. Restart() { super. on. Restart(); Log. d(tag, "In the on. Restart() event"); } CIS 470: Mobile App Development 8

Observe Activity Life Cycle Run => Debug, or public void on. Resume() { super.

Observe Activity Life Cycle Run => Debug, or public void on. Resume() { super. on. Resume(); Log. d(tag, "In the on. Resume() event"); } public void on. Pause() { super. on. Pause(); Log. d(tag, "In the on. Pause() event"); } public void on. Stop() { super. on. Stop(); Log. d(tag, "In the on. Stop() event"); } public void on. Destroy() { super. on. Destroy(); Log. d(tag, "In the on. Destroy() event"); } n Shift + F 9 to debug the app } 11/10/2020 CIS 470: Mobile App Development 9

Observe Activity Life Cycle n When the activity is first loaded, you should see

Observe Activity Life Cycle n When the activity is first loaded, you should see something very similar to the following in the logcat console n If you click the Back button on the Android emulator, you will see: 11/10/2020 CIS 470: Mobile App Development 10

Observe Activity Life Cycle n Click the Home button, click the Overview icon, select

Observe Activity Life Cycle n Click the Home button, click the Overview icon, select the Activity 101 app, you will see: n Click the Home button and then click the Phone button on the Android emulator so that the activity is pushed to the background n Exit the phone dialer by clicking the Back button, the activity is now visible again: 11/10/2020 CIS 470: Mobile App Development 11

Observe Activity Life Cycle: Summary n n Use the on. Create() method to create

Observe Activity Life Cycle: Summary n n Use the on. Create() method to create and instantiate the objects that you will be using in your application Use the on. Resume() method to start any services or code that needs to run while your activity is in the foreground Use the on. Pause() method to stop any services or code that does not need to run when your activity is not in the foreground Use the on. Destroy() method to free up resources before your activity is destroyed 11/10/2020 CIS 470: Mobile App Development 12

Intents n When your application has more than one activity, you often need to

Intents n When your application has more than one activity, you often need to navigate from one to another. In Android, you navigate between activities through what is known as an intent 11/10/2020 CIS 470: Mobile App Development 13

Linking Activities with Intents: Example n n Using Android Studio, create a new Android

Linking Activities with Intents: Example n n Using Android Studio, create a new Android project with an empty Activity named Main. Activity; name the project Using. Intent Right-click your package name under the java folder in the Project Files windows and select New ➪ Java Class Name the new class Second. Activity and click OK Add the bolded statements from the following code to the Android. Manifest. xml file 11/10/2020 CIS 470: Mobile App Development 14

Linking Activities with Intents: Example 11/10/2020 CIS 470: Mobile App Development 15

Linking Activities with Intents: Example 11/10/2020 CIS 470: Mobile App Development 15

Linking Activities with Intents: Example <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http:

Linking Activities with Intents: Example <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="com. jfdimarzio. usingintent"> <application android: allow. Backup="true”. . . <activity android: name=". Main. Activity">. . . </activity> <activity android: name=". Second. Activity" > <intent-filter > <action android: name="com. username. usingintent. Second. Activity" /> <category android: name="android. intent. category. DEFAULT" /> </intent-filter> </activity> </application> </manifest> 11/10/2020 CIS 470: Mobile App Development 16

Linking Activities with Intents: Example n n Make a copy of the activity_main. xml

Linking Activities with Intents: Example n n Make a copy of the activity_main. xml file (in the res/layout folder) by right-clicking it and selecting Copy. Then right-click the res/layout folder and select Paste. Name the file activity_second. xml Modify the activity_second. xml file as follows: <? xml version="1. 0" encoding="utf-8"? >. . . android: padding. Top="@dimen/activity_vertical_margin" tools: context="com. jfdimarzio. usingintent. Second. Activity"> <Text. View android: layout_width="wrap_content" android: layout_height="wrap_content" android: text="This is the Second Activity!" /> </Relative. Layout> 11/10/2020 CIS 470: Mobile App Development 17

Linking Activities with Intents: Example n In the Second. Activity. java file, add the

Linking Activities with Intents: Example n In the Second. Activity. java file, add the bolded statements from the following code: import android. app. Activity; import android. os. Bundle; public class Second. Activity extends Activity { public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_second); } } 11/10/2020 CIS 470: Mobile App Development 18

Linking Activities with Intents: Example n Add the bolded lines in the following code

Linking Activities with Intents: Example n Add the bolded lines in the following code to the activity_main. xml file: <? xml version="1. 0" encoding="utf-8"? > <Relative. Layout xmlns: android=http: //schemas. android. com/apk/res/android. . . <Text. View android: layout_width="wrap_content" android: layout_height="wrap_content" android: text="Main Activity!" android: id="@+id/text. View" /> <Button android: layout_width="wrap_content" android: layout_height="wrap_content" android: text="Display second activity" android: on. Click="on. Click" android: id="@+id/button" android: layout_below="@+id/text. View" android: layout_align. Parent. Start="true" android: layout_margin. Top="56 dp" /> </Relative. Layout> 11/10/2020 CIS 470: Mobile App Development 19

Linking Activities with Intents: Example n Modify the Main. Activity. java file as shown

Linking Activities with Intents: Example n Modify the Main. Activity. java file as shown in the bolded lines in the following code: import android. os. Bundle; import android. app. Activity; import android. content. Intent; import android. view. View; public class Main. Activity extends Activity { @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_main); } public void on. Click(View view) { start. Activity(new Intent("com. username. usingintent. Second. Activity")); } } 11/10/2020 CIS 470: Mobile App Development 20

Linking Activities with Intents: Example n n Press Shift+F 9 to debug the application

Linking Activities with Intents: Example n n Press Shift+F 9 to debug the application on the Android emulator When the first activity is loaded, click the button and the second activity also loads 11/10/2020 CIS 470: Mobile App Development 21

Homework #1 n Add a UI control on the screen of the second activity

Homework #1 n Add a UI control on the screen of the second activity so that you can go back to the first activity (i. e. , the main activity). In addition, on the main activity, display an iteration count on the number of times the main activity is displayed. 11/10/2020 CIS 470: Mobile App Development 22