Activities Flashcards
Activity
- An Activity is an application component. Represents one window, one hierarchy of views
- An activity typically has a UI layout -> layout is usually defined in one or more XML files
- An activity is Java class,
What does an Activity do?
- Represents an activity, such as ordering groceries, sending email, or getting directions
- Handles user interactions, such as button clicks, text entry, or login verification
- Can start other activities in the same or other apps
- Has a life cycle—is created, started, runs, is paused, resumed, stopped, and destroyed
Intent
An intent is a description of an operation to be performed.
INTENT - message to the Android runtime to perform an operation
An Intent is an object used to request an action from another app component via the Android system.
What can intents do?
— Start activities
A button click starts a new activity for text entry
Clicking Share opens an app that allows you to post a photo
— Start services
Initiate downloading a file in the background
— Deliver broadcasts
The system informs everybody that the phone is now charging
Explicit and implicit intents
Explicit Intent
Starts a specific activity
- Main activity starts the ViewShoppingCart activity
Implicit Intent
Asks system to find an activity that can handle this request
- Clicking Share opens a chooser with a list of apps
When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use.
Start an Activity with an explicit intent
Create an intent Intent intent = new Intent(this, ActivityName.class);
Use the intent to start the activity
startActivity(intent);
Start an Activity with implicit intent
Create an intent Intent intent = new Intent(action, uri);
Use the intent to start the activity
startActivity(intent);
EXAMPLES:
Show a web page
Uri uri = Uri.parse(“http://www.google.com”);
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
Dial a phone number
Uri uri = Uri.parse(“tel:8005551234”);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
Two types of sending data with intents
Data—one piece of information whose data location can be represented by an URI
SEND
intent.setData(
Uri.parse(“http://www.google.com”));
GET
getData();
—————————————————————–
Extras—one or more pieces of information as a collection of key-value pairs in a Bundle
SEND putExtra(String name, int value) ⇒ intent.putExtra("level", 406); putExtras(bundle); ⇒ if lots of data, first create a bundle and pass the bundle.
GET int getIntExtra (String name, int defaultValue) Bundle bundle = intent.getExtras();
Returning data to the starting activity
- Use startActivityForResult() to start the second activity
- To return data from the second Activity:
Create a new Intent
Put the response data in the Intent using putExtra()
Set the result to Activity.RESULT_OK
or RESULT_CANCELED, if the user cancelled out
call finish() to close the activity - Implement onActivityResult() in first activity
startActivityForResult()
startActivityForResult(intent, requestCode);
Starts activity (intent), assigns it identifier (requestCode) Returns data via intent extras When done, pop stack, return to previous activity, and execute onActivityResult() callback to process returned data Use requestCode to identify which activity has "returned"
Two forms of navigation
- – Temporal or back navigation
- – Ancestral or up navigation
Temporal or back navigation
- provided by the device’s back button
- controlled by the Android system’s back stack
Ancestral or up navigation
- provided by the app’s action bar
- controlled by defining parent-child relationships between activities in the Android manifest
What is the Activity Lifecycle?
The set of states an activity can be in during its lifetime, from when it is created until it is destroyed
Activity Lifecycle
https://google-developer-training.gitbooks.io/android-developer-fundamentals-course-concepts/content/en/images/2_2_C_images/basic-lifecycle.png
When does config change?
Configuration changes invalidate the current layout or other resources in your activity when the user:
- – rotates the device
- – chooses different system language, so locale changes enters multi-window mode (Android 7)
Activity instance state
State is lost when device is rotated, language changes, back-button is pressed, or the system clears memory