Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Department of Information Networks The University of Babylon LECTURE NOTES OF APPLICATION ACTIVITIES By Dr. Samaher Hussein Ali College of Information Technology, University of Babylon, Iraq [email protected] 5 November 2014 The APPLICATION Components • • • • Activity Service Broadcast receiver Content provider Typically, applications have one or more activities; and the main purpose of an activity is to interact with the user. From the moment an activity appears on the screen to the moment it is hidden, it goes through a number of stages, known as an activity’s life cycle. Understanding the life cycle of an activity is vital to ensuring that your application works correctly. Apart from activities, another unique concept in Android is that of an intent. An intent is basically the “glue” that enables different activities from different applications to work together seamlessly, ensuring that tasks can be performed as though they all belong to one single application. 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 The Topics 1. 2. 3. 4. 5. The activity class The task backstack The activity life cycle Starting activities Handling configuration changes 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 The Activity The activity Provides a visual interface for user interaction Each activity typically supports one focused thing a user can do, such as • Viewing an email message • Showing a login screen Note : Android supports navigation in several ways: 1. 2. 3. Tasks The task backstack Suspending & resuming activities 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 1. A Task is a set of related Activities: These related activities don’t have to be part of the same application Most tasks start at the home screen • See: http://developer.android.com/guide/topics/fundamentals/tasksand-back-stack.html 2. The Task Backstack When an Activity is launched, it goes on top of the backstack When the Activity is destroyed, it is popped off the backstack 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 UNDERSTANDING ACTIVITIES • To create an activity. To create an activity, you create a Java class that extends the Activity base class: package net.learn2develop.Activity101; import android.app.Activity; import android.os.Bundle; public class Activity101Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } Your activity class loads its UI component using the XML file defined in your res/layout folder. In this example, you would load the UI from the main.xml fi le: setContentView (R.layout.main); Every activity you have in your application must be declared in your AndroidManifest.xml fi le, like this: 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 Declare of each Activaty in application <?xml version=“1.0“ encoding=“utf-8“?> <manifest xmlns:android=“http://schemas.android.com/apk/res/android“ package=”net.learn2develop.Activity101” android:versionCode=”1” android:versionName=”1.0” > <uses-sdk android:minSdkVersion=”14” /> <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” > <activity android:label=”@string/app_name” android:name=”.Activity101Activity” > <intent-filter > <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application> </manifest> 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 Declare of each Activaty in application Within the definition for this activity, there is an element named <intent-filter>: The action for the intent filter is named android.intent.action.MAIN to indicate that this activity serves as the entry point for the application. The category for the intent-filter is named android.intent.category.LAUNCHER to indicate that the application can be launched from the device’s launcher icon. • The Activity base class defines a series of events that govern the life cycle of an activity. Activity class defines the following events: onCreate() — Called when the activity is first created onStart() — Called when the activity becomes visible to the user onResume() — Called when the activity starts interacting with the user onPause() — Called when the current activity is being paused and the previous activity is being resumed onStop() — Called when the activity is no longer visible to the user onDestroy() — Called before the activity is destroyed by the system (either manually or by the system to conserve memory) onRestart() — Called when the activity has been stopped and is restarting again 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 The Activity Life Cycle 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 The Activity Life Cycle 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 The Activity Life Cycle 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 The Activity Life Cycle 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 Functions OnCreate() Called when Activity is created Sets up Initial state Call super.onCreate() Set the Activity’s content view Retain references to UI views as necessary Configure views as necessary • Called if the Activity has been stopped • and is about to be started again • Typical actions • Special processing needed only after • having been stopped 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 Recall of some activities • protected void onCreate (Bundle savedInstanceState) • protected void onStart() • protected void onResume() • protected void onPause() • protected void onRestart() • protected void onStop() • protected void onDestroy() Example: apply the Map Location on the class room. • <activity android:name=".MyActivity“" • android:configChanges=" • "orientation|screensize|keyboardHidden”…> 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3 Configuration Changes Activity restarting should be fast If necessary you can: 1. Retain an Object containing important state information during a configuration change 2. Manually handle the configuration change <activity android:name=".MyActivity“ "android:configChanges=" "orientation|screensize|keyboardHidden”…> 5 November 2014 Dr. Samaher Hussein Ali Notes of Lecture #3