MobAppDev (Fall 2014): Activity Lifecycle Management, Intent Resolution, Implicit Intents, Data...
-
Upload
vladimir-kulyukin -
Category
Science
-
view
185 -
download
0
description
Transcript of MobAppDev (Fall 2014): Activity Lifecycle Management, Intent Resolution, Implicit Intents, Data...
MobAppDev
Activity Lifecycle Management, Intent Resolution, Implicit Intents,
Data Transfer with Intents
Vladimir Kulyukin
www.vkedco.blogspot.com
Outline● Activity Lifecycle Management● Explicit & Implicit Intents● Intent Resolution● Data Transfer with Intents
Activity Lifecycle Management
Review: 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() { }
}
Source: http://developer.android.com/guide/components/activities.html#Lifecycle
Review: Activity's Lifecycle Diagram
Source:http://developer.android.com/training/basics/activity-lifecycle/starting.html
Review: Activity's Lifecycle Diagram
Review: 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) killable 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 Manag-er, 3) killable when memory is needed somewhere else
Activity Lifecycle Management: Example
● Develop an app that has two Activities: the first activity allows the user to sign in with the user name and password
● If the user name and password are correct, the second activity is launched that displays the list of friends of the user who signed in
● The activity overrides all lifecycle management methods and logs appropriate messages in LogCat
● Source code is here
Application Screenshots
Explicit Intents
● If Android OS resolves an explicit intent via its Java class identifier & the component specified by the class identifier is accessible, the component will likely be launched
● Explicit intents are typically used within one application when the developers have control over the Java class identifiers of the components
Data Transfer with Intents
Data Transfer with Intents
● Intents can bundle and transfer data to other components via Android
● An intent sender creates an intent, puts 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
Putting Data into Explicit Intents
@Override
public void onClick(View v) {
Intent i=new Intent(getApplicationContext(),ListOfUserFriends.class);
// you would have to encrypt user name and password here
i.putExtra("username", mEdTxtUsername.getText().toString());
i.putExtra("password", mEdTxtPwd.getText().toString());
startActivity(i);
}
Extracting Data from Explicit Intents
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent profile = getIntent();
final String userName = profile.getStringExtra("username");
final String password = profile.getStringExtra("password");
…
}
Implicit Intents
Implicit Intents● Implicit intents provide a mechanism for anonymous (no
explicit activity specification) requests ● An activity asks Android OS to do an action by specifying
high-level requirements but without knowing which activity or application will actually perform the action
● The requesting action can specify a URI to the data which the performing activity must process or add that data to the implicit intent
Native Android Actions● Android OS defines many actions that can be satisfied by native or 3-rd
party components and are launched via implicit intents● Here are some examples:
ACTION_ANSWER – open an activity that handles incoming calls ACTION_CALL – open an activity with a phone dialer ACTION_PICK – launch an activity to pick an item from a list of
items ACTION_WEB_SEARCH – launch an activity to perform a web
search ACTION_VIEW – launch an activity that can view the specified data
in a reasonable manner
Implicit Intents
● Implicit intents provide a mechanism for anonymous requests
● An implicit intent is launched with startActivity(Intent i)● Activities and applications use intent filters to tell Android
ecosystem about the actions they respond to and/or which data types they work with
● An component that has an intent filter is called an intent filter
Intent Filter's Structure● Action
Name of the action being serviced Name should be a unique string Best practice is to use the Java package naming conven-
tion ● Category
When the action should be serviced● Data
Data types that the action can work with
Intent Resolution● Intent resolution starts when an activity calls
startActivity() or startActivityForResult() with some intent Intnt
● Android OS gets the list of all intent filters available from the installed packages
● Intent filters that do not match Intnt's requested action or category are removed from the list
Intent Resolution● An action match occurs when Intnt's action and the
filter's action are the same or no action is specified● A category match occurs when Intnt's categories
and the filter's categories are the same● A data match occurs when Intnt's data types are the
same as the filter's data types
Intent Resolution● Native Android components are part of the Intent resolution
process● Native components are no different from 3rd-part components● Native components do not have higher-priorities and can be
completely replaced with 3rd-party components with the user's consent
● Android ecosystem does not give anyone a priori advantages to any components
Implicit Intent Example● Develop an app that uses an implicit intent to request Android
ecosystem to find a component that can display the wiki entry about Kurt Gödel
● This implicit intent must contain two components: Intent.ACTION_VIEW and the URI (e.g., URL of a web page)
● The default native component that responds to this intent is the web browser
● Source code is here
Sample Screenshot
Code Fragments
Wiki Constant in /res/values/strings.xml
<resources>
...
<string name=”kurt_godel_wiki_url”>
http://en.wikipedia.org/wiki/Kurt_Godel
</string>
...
</resources>
Launching Intent for Viewing URL
public class GodelWikiPageBrowserAct extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.godel_wiki_page_browser_act_layout);
Intent myIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(getResources().getString(R.string.kurt_godel_wiki_url)));
this.startActivity(myIntent);
}
…
}