View
142
Download
1
Category
Preview:
DESCRIPTION
Citation preview
MobAppDev
Activities, Applications, Intents
Vladimir Kulyukin
www.vkedco.blogspot.com
Outline● Activities
Lifecycle Lifetimes Essential States Callback Methods
● Applications on Android OS Dalvik VM JIT Android Runtime AOT Principle of Least Privilege
● Intents
Basic Concepts
Activity● Activity is an application screen (aka a component)● Activity provides the user a screen with which the user can interact● Each activity is assigned a window in which it can draw its interface● One activity can start another activity● When an new activity starts it is pushed on the back stack and
receives user focus● The back stack is a last-in-first-out data structure
Application
● Applications are packaged executables (.apk files)● All application code, data, and resources reside in a single archive
.apk file● Applications consist of one or more components, such as activites,
services, content providers, and broadcast receivers● The manifest file must declare all components of the application and
declare all application requirements
Process & Program
● A process is an executing/running instance of a program● In OS literature, processes are frequently referred to as tasks● A program is an executable file stored in external memory, e.g., hard disk
drives● An executable file is a binary file that has been compiled from source code
(Java) into machine code (Dalvik JVM byte code)● A program is a passive entity until it starts to run, i.e., becomes a process or is
mounted on a running process
Activities
Activity's Lifecycle
● An actvity's lifecycle is a set of states and transitions● One can think of the lifecycle as a finite state machine (FSA)● When the current state of an activity changes, Android OS notifies the activity of
that change so that the appropriate callback method is called (if implemented)● The Android developer manages the activity's lifecycle by implementing the
standard callback methods called on the activity object when its state changes (e.g., activity is created, stopped, resumed, and destroyed)
● Callback implementation is the only leverage the developer has over activities
Activity's Lifecycle: Callback Methodspublic class MyActivity {
// The activity is created
public void onCreate(Bundle savedInstanceState) { }
// The activity is about to become visible
protected void onStart() { }
//The activity is visible (it is resumed)
protected void onResume() { }
// Another activity is taking focus (it is paused)
protected void onPause() { }
// The activity is no longer visible (it is stopped)
protected void onStop() { }
// The activity is about to be destroyed
protected void onDestroy() { }
}
Image Source: http://developer.android.com/guide/components/activities.html#Lifecycle
Activity's Lifecycle Diagram
Activity's Lifetimes
● Entire lifetime – from onCreate() to onDestroy()● Visible lifetime – from onStart() to onStop(): user sees the activity and
interacts with it● Foreground lifetime – from onResume() to onPause(): activity is in
front of all other activities on screen and has user focus; code in onResume() and onPause() should be lightweight because activities frequently transition in and out of foreground
Activity's Lifetimes
Activity's Three Basic States
● Running/Resumed – this activity is in the foreground of the screen and receives user focus
● Paused – This activity is partially obscured by another activity that gets into the foreground and receives user focus
Paused activity objects are 1) in memory, 2) attached to Window Manager, 3) kil-lable only in extremely low memory conditions
● Stopped – This activity is completely obscured by another activity Stopped activity objects are 1) in memory, 2) detached from Window Manager, 3)
killable when memory is needed somewhere else
Some Details on Callback Methods
Activity.onCreate()● onCreate() is called when the activity is created● onCreate() is the place to create views and do data binding● onCreate() takes a Bundle that contains the previous state of the activity if that
state was persisted● onCreate() is followed by onStart()● After this method returns, the hosting process cannot be killed w/o executing
another line of the activity's code, i.e., the hosting process is not killable● An activity is killable from the time onPause() returns to the time onResume()
is called
Activity.onRestart()
● onRestart() is called after the activity is stopped and right before it is started again
● onRestart() is followed by onStart()● After this method returns, the hosting process cannot be
killed w/o executing another line of the activity's code, i.e., the hosting process is not killable
Activity.onStart()
● onStart() is called when the activity is visible to the user● onStart() is followed 1) by onResume() if the activity gets
into foreground and 2) by onStop() if it becomes hidden● After this method returns, the hosting process cannot be
killed w/o executing another line of the activity's code, i.e., the hosting process is not killable
Activity.onResume()
● onResume() is called right before the user begins to interact with the activity; the activity is taking user input
● onResume() is followed by onPause()● After this method returns, the hosting process cannot be
killed w/o executing another line of the activity's code, i.e., the hosting process is not killable
Activity.onPause()● onPause() is called right before Android OS resumes a different activity● onPause() is used to persist data, stop CPU consumables (animations,
downloads, etc.)● onPause() is followed 1) by onResume() if the activity returns back to front or
2) by onStop() if it becomes hidden Pre 3.0: The hosting process can be killed after this method returns
without executing another line of the activity's code
Post 3.0: The hosting process cannot be killed after this method re-turns without executing another line of the activity's code
Activity.onStop()
● onStop() is called when the activity is no longer visible to the user because 1) Android OS begins to destroy the activity or 2) another activity is started or resumed and is covering this activity
● onStop() is followed by 1) onRestart() if the activity is coming back to the foreground or 2) onDestroy() if the activity is destroyed
● After this method returns, the hosting process can be killed w/o executing another line of the activity's code, i.e., the hosting process is killable
Activity.onDestroy()● onDestroy() is called when the activity is destroyed● onDestroy() is the final call the activity receives before it is
destroyed by the OS● onDestroy() is called 1) when the activity is finishing after a call to
Activity.finish() or 2) Android OS is destroying the activity to save space
● After this method returns, the hosting process can be killed w/o executing another line of the activity's code, i.e., the hosting process is killable
Applications & Android OS
Apps on Android OS● Android OS is a multi-user Linux system● Each app is a different user● Android OS assigns each app a unique Linux user ID● The user ID is known to the OS but is not known by the app● The OS sets permissions based on the user ID● Every application, by default, runs in its own Linux process: the OS starts
this process when one of the app's components is executed● Each process has its own Dalvik JVM: each application's running code runs
isolated from other apps
Android Runtime (ART)● Android Runtime (ART) is an application runtime environment for
Android OS● ART replaces Dalvik VM as of Android L● Main difference b/w Dalvik VM and ART is JIT compilation (on Dalvik
VM) and AOT compilation (on ART)● The claim is that AOT is more efficient than JIT
Process Sharing● Apps may share the same Linux user ID and, consequently, share
each other's files and resources● Apps with the same user ID can also request to run in the same
Linux process and share the same JVM: this is done when the resources must be conserved
● Apps may request permission to access data stored on the device, e.g., user contacts, Bluetooth, SD card, SMS messages, etc
● All permissions are granted at install time
Principle of Least Privilege● PLP principle: Each app has access only to those
components required to do its work and no more● An app cannot access parts of the system for which it is not
given permission● Permissions must be explicitly declared in the manifest file
Intents
Overview
● Intents are messages that allow Android components (e.g., activities, applications, etc) to send computational requests or notifications to other Android components via Android OS
● Intents are used for requesting information, computation, or event signaling
● All intents objects are instances of android.content.Intent
Explicit and Implicit Intents
● Intents may contain data: one activity may send data to another activity
● Android supports two types of intents: explicit or implicit● Explicit intents must know the Java class name of the component
that they want to start● Implicit intents must know the action type they want to be done for
them
Explicit Intents
● If Android OS successfully resolves an explicit intent if 1) its Java class identifier is valid & 2) the component specified by the class identifier is accessible, i.e., the component can be launched
● Explicit intents are typically used within one application when the developers have control over the Java class identifiers of the components
Explicit Intent Example
● Intent i = new Intent(this, ImageDisplayAct_01.class);
● If ImageDisplayAct_01.class is resolved, the corresponding component is launched
● If not, there is a run-time error
Implicit Intents● Implicit intents must specify the type of action they should perform
and possibly the type of data● If there is a component in the Android ecosystem that is registered to
perform the specified action type, it will likely be launched● If there are several components in the Android ecosystem that are
registered, Android will ask the user to select one via a selection dialog
● If no component is found, nothing happens
Implicit Intent Example
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
● If there is a component registered under MediaStore.ACTION_IMAGE_CAPTURE, it will likely be launched
● Multiple components may be registered to perform the same action
Data Transfer with Intents
● Both types of intents can bundle and transfer data to other components via Android
● An intent sender creates an intent, put a few key-value pairs into its bundle and sends it to Android
● An intent receiver receives an intent, checks it for the presence/absence of specific keys and then does some action
● Keys are always Strings
Explicit Intents: Example 01
● Develop an app that has two Activities: MainLauncher and ImageDisplayerAct01
● The MainLauncher activity consumes a button click and launches the ImageDisplayerAct01 activity via an explicit intent
● The ImageDisplayerAct01 activity displays a random image with an ImageView object
● We will call this project ExplicitIntentsApp01
ExplicitIntentsApp01Code Snippets
source code is here
MainLauncherAct.onCreate()protected Button mBtnDisplay = null;
protected Activity mThisAct = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_launcher);
mBtnDisplay = (Button) this.findViewById(R.id.btn_display);
mThisAct = this;
// Button click creates an explicit intent to request a launch of ImageDisplayerAct01
mBtnDisplay.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
// 1. Create an explicit intent
Intent i = new Intent(mThisAct, ImageDisplayerAct01.class);
// 2. Request Android to run it.
startActivity(i); }});
}
ImageDisplayerAct01.onCreate()
protected ImageView mImgView = null;
protected void onCreate(Bundle savedInstanceState) {
mImgView = (ImageView) this.findViewById(R.id.img_view_01);
mImgView.setImageDrawable(null);
int img_num = new Random().nextInt(8) + 1;
switch ( img_num ) {
case 1: mImgView.setBackgroundResource(R.drawable.img_01); break;
case 2: mImgView.setBackgroundResource(R.drawable.img_02); break;
// Other cses for 3 to 8
}
Toast.makeText(this, getResources().getString(R.string.randome_img_msg) + " " + img_num,
Toast.LENGTH_SHORT).show();
}
Explicit Intents: Example 02● Develop an app that has two Activities: MainLauncher and
ImageDisplayerAct02
● The MainLauncher activity consumes a button click and launches the ImageDisplayerAct02 activity via an explicit intent AND passes to the ImageDisplayerAct02 the numerical ID of the image to display
● The ImageDisplayerAct02 activity displays the requested image in an ImageView object
● We will call this project ExplicitIntentsApp02
ExplicitIntentsApp02Code Snippets
source code is here
MainLauncherAct.onCreate() mBtnDisplay.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
// 1. Create an explicit intent
Intent i = new Intent(mThisAct, ImageDisplayerAct02.class);
// 2. Generate a random number in [1, 8]
int random_img_id = new Random().nextInt(8) + 1;
// 3. Put key-value pair into the intent.
i.putExtra(mRes.getString(R.string.img_id_key), random_img_id);
// 4. Toast which image is requested.
Toast.makeText(mThisAct,
mRes.getString(R.string.img_display_request_msg) + " "
+ random_img_id, Toast.LENGTH_SHORT).show();
// 5. Request Android to run it.
startActivity(i);}});
}
ImageDisplayerAct02.onCreate()// 1. Get the intent
Intent i = this.getIntent();
// 2. Check to see if it contains an integer keyed on R.string.img_id_key.
// default to 0 if it does not.
int img_num = i.getIntExtra(mRes.getString(R.string.img_id_key), 0);
// 3. If it does, display it
if (img_num > 0) {
switch (img_num) {
case 1: mImgView.setBackgroundResource(R.drawable.img_01); break;
case 2: mImgView.setBackgroundResource(R.drawable.img_02); break;
// handle other cases from 3 to 8
}
// Set the text of the mTV text view to the appropriate message.
mTV.setText(mRes.getString(R.string.img_display_msg) + " " + img_num);
References
● http://developer.android.com/reference/android/app/Activity.html● http://developer.android.com/guide/components/fundamentals.html● http://developer.android.com/guide/components/processes-and-threads.html● http://www.linfo.org/process.html● http://en.wikipedia.org/wiki/Sandbox_(software_development)● http://en.wikipedia.org/wiki/Sandbox_(computer_security)
Recommended