Platform-Base Development-Week2 Notes PDF

Title Platform-Base Development-Week2 Notes
Author Geo Qujad
Course Platform Based Development
Institution Colorado State University - Global Campus
Pages 11
File Size 763 KB
File Type PDF
Total Downloads 98
Total Views 138

Summary

Download Platform-Base Development-Week2 Notes PDF


Description

Platform-Based Development Module 2: Android App Development Basics Module 2 introduces some of the basic concepts and first steps associated with building applications in Android Studio. You will become acquainted with the user interface components of Android Studio, create a simple application, and launch an application in the Android emulator. Learning Outcomes 1. 2. 3. 4. 5.

Create a new application project. Construct a graphical user interface (GUI). Display text and image elements within an application. Edit views for an application. Launch an app using the Android emulator.

1. Understanding Activities in Android Development Introduction to Activities In Android Studio, you will be working with points of interests that are called activities. Activities provide the main mechanism by which users are able to interact with in an application. An application can consist of various activities that are bound together to create the functionality that is required for an app. When an app launches, the main screen is presented as an activity that provides for the mechanisms by which a user can interact with other components or activities within the application. Twelve Things to Know about Activities within the Application • •

• •

Item #1: An Android application is a loosely bound collection of activities, services, and other components. Item #2: o Packaged in an .apk file: o An archive of all components in the application. Item #3: o Described by a manifest: o Lists and configures the key application components. Item #4: o Android OS launches a Linux process for each application: o Assigns each application a unique Linux user ID. o Gives privileges to that user. o Launches each application in a separate Java VM. o This provides a “least privilege” environment:

• •

• • •

• •



o Maximizes security. Item #5: Applications are normally started by the user launching an activity. Item #6: o An activity is a component with which the user can interact: o Associated with a screen displaying the user interface. o The contents of the screen are defined by a view: § Normally defined in an XML resource file. § XML = extensible markup language Item #7: o Applications normally consist of multiple activities: o One for each “screen” in the application. Item #8: Activities launch other activities by sending an intent to the application framework. Item #9: o When a new activity is started, it is placed on the back stack: o The previous activity is stopped. o Pending reactivation when the user presses the Back button. Item #10: When Back is pressed, the current top activity is removed and destroyed. Item #11: Once created, activities are in one of three main states: o Resumed: § Activity is visible and running. o Paused: § Activity is partially obscured by another. § The activity is alive but may be killed if the OS is very short on resources. o Stopped: § Activity is now in the background—completely obscured. § State is maintained (all member variables and connections, for example). § Can be killed if the OS needs the memory. Item #12: o Application framework provides life cycle events: o Notify activity as its state changes.

Methods for Activities Android activities are controlled by utilizing a number of different methods which help control the flow of the activity, application, and user interaction. The onCreate() method is utilized when an activity is first created and is considered the starting point for interaction with the application. • The onStart() method is utilized and called once components of an activity are made viewable by an end-user of an application. Figure 1 provides an overview of a sample method call flow for a given activity. Per Android Developers, “the square rectangles represent callback methods you can implement to perform operations when the activity moves between states. The colored ovals are major states the activity can be in” (Activity, n.d., para. 11). •

https://developer.android.com/reference/android/app/Activity.html Figure 1. Activity Call Trace Overview



Methods Used in the Lifecycle of an Activity: The onCreate() and onStart() methods are two of the first methods utilized in initializing an Android application.



Methods for Carrying Out the Lifecycle of an Activity: Depending on the status of the interaction with a user and the application, the onREsume(), onPause(), and onStop() methods can be utilized to interact with an activity: o The onResume() method is used to restart the interaction between a user and an application activity.

o The onPause() method can be utilized in order to switch between appropriate activities. Specifically, if a user leaves one activity and starts interacting within another, then the onPause() method will be called. o Just as the onStart() method is called to make an activity visible to a user, the onStop() method is used in order to hide an activity, or make it no longer viewable to a user in an application. Developing an activity involves the following steps. •

Step #1: An activity is a subclass of android.app.Activity:

Figure 2: WeatherActivity is a subclass of Activity base class. •

• • •

Step #2: Override the onCreate() method: o Use like a constructor. o Called by framework when the environment is stable. Step #3: Call the onCreate() method in the superclass. Step #4: Set up a view for the activity: o Pass the ID of a resource definition. o Pass a reference to a view object. Step #5: Override other life cycle methods as needed.

Figure 3 depicts the Java code for a minimal Android activity. In the code snippet, “WeatherActivity” is a subclass of the “Activity” base class. The WeatherActivity onCreate() method overrides the base class override method. The View for the Activity Views are commonly defined using XML declarations. Three Facts about Views • •



Fact #1: They are stored under the res directory of the application. Fact #2: File path and name provide the Id of the resource: o Path: res/layout/main.xml o Resource ID: R.layout.main Fact #3: R is an autogenerated class representing the resources.

Figure 4 depicts the linear layout XML code for the WeatherActivity.

The Android Manifest Three Things to Know about the Android Manifest • •



Item #1: Every Android application must have a manifest: AndroidManifest.xml Item #2: The manifest provides a catalog of the application components: o Activities o Services o Security permissions o Content providers o Intents to respond to. Item #3: Components cannot be executed unless they are present in the manifest.

Supporting Android Functionality on Older Devices Three Things to Know about Supporting Android Functionality on Older Devices • • •

Item #1: New Android versions add major new features: o Applications commonly need to run on older versions. o Implemented using Android compatibility libraries. Item #2: Android Studio projects automatically support backward compatibility: o Based on API versions selected. Item #3: To support fragments and the action bar: o Compatibility library uses a sub-class of Activity.AppCompatActivity

2. Understanding GUI Development for Android Apps

https://cdn.pixabay.com/photo/2015/05/21/18/52/tablet777652_960_720.jpg Android GUI Basics Building graphical user interfaces (GUI) in Android Studio can be done by utilizing a variety of components that make UI building quick and easy. Android Studio has a visual design editor that

can be utilized to arrange required UI components. These components can then be previewed based upon the device to be utilized and the version of Android OS that the device utilizes. The Layout Editor When first launched, the Layout Editor in Android Studio will open the activity_main.xml file. The activity_main.xml file is used as a guideline for establishing the components required to create an appropriate user interface. The interactive exercise below highlights some of the main components of the Layout Editor in Android Studio. Hover over each numbered item to learn about these key components.

Item 1 provides an overview of the appropriate widgets and layouts that can be used to build the user interface for the application. • Item 2 provides a hierarchical view of the layout showing all of the activity components that are being utilized, such as header, favorite, and title, for example. • Item 3 provides an overview of the toolbar, which allows for the developer to configure the layout editor’s appearance. • Item 4 provides an overview of the design editor window, which will give a mockup of the layout that will be presented in the app. • Item 5 on the image provides a view of the current properties that are being displayed for the selected item in the design editor. The properties view will allow for modifications to be made to components such as image size, background, and other scaling components for the layout. Component Layouts in Android Studio •

Android Studio includes layouts that can be used to quickly build layouts. However, sometimes it is helpful to declare your UI elements in XML because it “enables you to better separate the

presentation of your application from the code that controls its behavior” (Android Developers, n.d., para. 5). In customizing appropriate layout properties, it is important to have a good understanding of the orientation that will be required for the application’s user interface. Since images and other components might not scale well within the application, a thorough understanding of the properties associated with the interface is required. The LinearLayout properties can be adjusted to scale the orientation of interface components, as needed. Other components that will need to be changed in the user interface include the TextView, and ImageView. 3. Understanding Services and Intents Keeping the System Responsive Six Things to Know about Keeping the System Responsive • • •



Items #1: Mobile devices are relatively limited in performance: o CPU speeds are kept low to improve battery life. Items #2: Users want apps that function smoothly. Items #3: Developers must focus on developing responsive applications. o No long pauses while: o Waiting for a network or GPS position. o Searching databases and writing files, for example. Items #4: A single resource-hogging application can slow everything down:



Android has a mechanism for detecting nonresponsive applications. The dreaded Application Not Responding (ANR) error.

• •

Items #5: To avoid ANRs, do not perform long-running tasks in the primary user interface thread. Items #6: Android provides several mechanisms to run background tasks: o Services o AsyncTask o Threads

o Intents. Services

1. Run in the background to perform long-running actions. Services continue to run after any associated activities have been destroyed. 2. Services are launched from the main UI thread. They must start a thread to perform long-running actions. 3. IntentService is a subclass that automatically launches a thread. Place long-running code in void onHandleIntent(Intent intent). Code in this method automatically runs in a background thread. • •

Item #1 Activities and services cannot be started directly. Item #2: You must request that the framework launch them: o Create an intent: § Specify the class to launch. o Send it to the framework: § Call startService() or startActivity().



Item #3: Activities, services, and other components interact with the application framework via the Context object: android.content.Context.

Figure 6 shows a code snippet that demonstrates how to launch a given activity using an intent

. Figure 7 is a UML diagram that shows how a context aggregates activity and service classes for processing by the Android application framework. 4. Android Logging • • •

Item #1: Logging is an effective way of recording system behavior. Logging is used to record significant events and errors. Item #2: Android provides a pre-built logging API similar to Log4J. Item #3: Android logs are held in device memory—not files: o LogCat (an SDK tool) displays logs during development. o LogCat output is displayed in the Android Window of Android Studio.

Figure 8: Android Studio logcat output. The Logging API Five Things to Know about the Logging API •

• •

• •

Item #1: Static methods provided on the android.util.Log class: o Log.v (verbose), Log.d (debug), Log.i (information) o Log.w (warning), Log.e (error). Item #2: Logging methods share common signatures: o Log.w(String tag, String msg) o Log.w(String tag, String msg, Throwable tr). Item #3: Good practice is to define the log tag as a constant in the class: o Private static final String TAG="IntentService"; o Use the name of the class as tag value o Unless you have a more meaningful one in mind. Item #4: Add logging wherever needed during development: o But remember, there is a performance cost: o Log.i (TAG, "Something interesting happened"); Item #5: Debug and verbose logging should always be stripped out of production applications.

References: Android Developers. (n.d.). Activity. Retrieved from https://developer.android.com/reference/android/app/Activity.html

Android Developers. (n.d.). Layouts. Retrieved from https://developer.android.com/guide/topics/ui/declaring-layout.html...


Similar Free PDFs
Notes
  • 18 Pages
Notes
  • 12 Pages
Notes
  • 61 Pages
Notes
  • 35 Pages
Notes
  • 19 Pages
Notes
  • 70 Pages
Notes
  • 6 Pages
Notes
  • 35 Pages
Notes
  • 29 Pages
Notes
  • 70 Pages
Notes
  • 6 Pages
Notes
  • 19 Pages
Notes
  • 32 Pages
Notes
  • 28 Pages
Notes
  • 56 Pages