Anda di halaman 1dari 27

Android Component 1

Marthin Satrya Pasaribu


Outline
• Android Architecture
• Basic Components
• Activity
• Thread
• Fragment
What is Android
• A software stack for mobile devices that includes
– An operating system
– Middleware
– Key Applications
• Uses Linux to provide core system services
– Security
– Memory management
– Process management
– Power management
– Hardware drivers
Application Components
Five primary components, different purposes and different lifecycles
1. Activity
An Activity is an application component that provides a screen with which users can interact in
order to do something, such as dial the phone, take a photo, send an email, or view a map.
Single screen with a user interface

Subclass of Activity super class


App may have several activities
2. Intents
used to pass information between applications
three fundamental use-cases:
–To start an activity

–To start a service

–To deliver a broadcast


• Service
• Application component that performs long-running operations in background with
no UI
• Example, an application that automatically responds to texts when driving
• Content Providers
• A bridge between applications to share data
• A content provider presents data to external applications as one or more tables that
are similar to the tables found in a relational database.
• A row represents an instance of some type of data the provider collects, and each
column in the row represents an individual piece of data collected for an instance.
• For example the devices contacts information; we tend to use these, but not create
new ones
• Broadcast Receivers
• component that responds to system wide announcements
• battery low, screen off, date changed
• also possible to initiate broadcasts from within an application
Starting Activities
• Android applications don't start with a call to main(String[])
• instead a series of callback methods are
• invoked
• each corresponds to specific stage of the Activity / application
lifecycle
• callback methods also used to tear down Activity / application
Android Lifecycle
An activity has essentially four states:
• If an activity in the foreground of the screen (at the top of the stack), it is active or
running.
• If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent
activity has focus on top of your activity), it is paused. A paused activity is completely
alive (it maintains all state and member information and remains attached to the
window manager), but can be killed by the system in extreme low memory situations.
• If an activity is completely obscured by another activity, it is stopped. It still retains all
state and member information, however, it is no longer visible to the user so its window
is hidden and it will often be killed by the system when memory is needed elsewhere.
• If an activity is paused or stopped, the system can drop the activity from memory by
either asking it to finish, or simply killing its process. When it is displayed again to the
user, it must be completely restarted and restored to its previous state.
Android Lifecycle (2)
Activity Stack
Activity 1

Activity 2

Activity 3

Activity N
Android Manifest
LifeCycleTest
• Run the app and open the Logcat
• Every Android log message has a tag and a priority
associated with it.
• You define it in a Log method call, for example:
Log.d(tag, message);
• The priority is one of the following values:
• V — Verbose (lowest priority)
• D — Debug
• I — Info
• W — Warning
• E — Error
• A — Assert
Logcat
• Rotate emulator with CTRL+F11
Stopping – onStop()
• Many scenarios cause activity to be stopped
• Well behaved apps save progress and restart seamlessly
• Activity stopped when:
• user performs action in activity that starts another activity in the application
• user opens Recent Apps window and starts a new application
• user receives phone call
• use onStop to release all resources and save information
(persistence)
How to stop an Activity yourself?
•Generally, don't worry about it!
•"Note: In most cases, you should not explicitly finish an activity
using these methods. As discussed in the following section
about the activity lifecycle, the Android system manages the life
of an activity for you, so you do not need to finish your own
activities. Calling these methods could adversely affect the
expected user experience and should only be used when you
absolutely do not want the user to return to this instance of the
activity."
•methods: finish(), finishActivity()
Saving State
• Activities that are paused or stopped the state (instance vars) of the
activity are retained
• even if not in foreground
• When activity destroyed, the Activity object is destroyed
• can save information via onSaveInstanceState method. Write data to Bundle,
Bundle given back when restarted
Activity Destruction
• App may be destroyed under normal circumstances
• on its own by calling finish or user pressing the back button to navigate away
from app
• normal lifecycle methods handle this onPause() -> onStop() -> onDestroy
• If the system must destroy the activity (to recover resources or on an
orientation change) must be able to recreate Activity
Activity Destruction
Activity Destruction
• If Activity destroyed with potential to be recreate later, system calls
the onSaveInstanceState (Bundle outState) method
• Bundle is a data structure like a Map
• String keys
• put methods for primitives, arrays, Strings, Serializables (Java), and Parcels
(android)
UI Thread
• What is Thread?
• When application was running, as a default Android system will run a thread/
process
• One android application will be running on a thread, it’s called main
thread/ UI thread on android system
• What is the purpose of thread?
• There are two ways to execute code in a new thread.
• You can either subclass Thread and overriding its run() method, or
• construct a new Thread and pass a Runnable to the constructor.
• In either case, the start() method must be called to actually execute
the new Thread.
Fragment
•A Fragment represents a behavior or a portion of user
interface in an Activity.
•You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities.
•You can think of a fragment as a modular section of an
activity, which has its own lifecycle, receives its own input
events, and which you can add or remove while the activity is
running (sort of like a "sub activity" that you can reuse in
different activities).
Fragment (2)
• A fragment must always be embedded in an activity and the fragment's lifecycle
is directly affected by the host activity's lifecycle.
• For example, when the activity is paused, so are all fragments in it, and when the
activity is destroyed, so are all fragments.
• However, while an activity is running (it is in
• the resumed lifecycle state), you can manipulate each fragment independently,
such as add or remove them.
• When you perform such a fragment transaction, you can also add it to a back
stack that's managed by the activity— each back stack entry in the activity is a
record of the fragment transaction that occurred.
• The back stack allows the user to reverse a fragment transaction (navigate
backwards), by pressing
• the Back button.