Activities Flashcards

1
Q

Activity

A
  • 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,
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does an Activity do?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Intent

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What can intents do?

A

— 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explicit and implicit intents

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Start an Activity with an explicit intent

A
Create an intent
Intent intent = new Intent(this, ActivityName.class);

Use the intent to start the activity
startActivity(intent);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Start an Activity with implicit intent

A
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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Two types of sending data with intents

A

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();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Returning data to the starting activity

A
  1. Use startActivityForResult() to start the second activity
  2. 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
  3. Implement onActivityResult() in first activity
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

startActivityForResult()

A

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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Two forms of navigation

  • – Temporal or back navigation
  • – Ancestral or up navigation
A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the Activity Lifecycle?

A

The set of states an activity can be in during its lifetime, from when it is created until it is destroyed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Activity Lifecycle

A

https://google-developer-training.gitbooks.io/android-developer-fundamentals-course-concepts/content/en/images/2_2_C_images/basic-lifecycle.png

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When does config change?

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Activity instance state

A

State is lost when device is rotated, language changes, back-button is pressed, or the system clears memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Saving instance state

A

Implement onSaveInstanceState() in your activity

called by Android runtime when there is a possibility the activity may be destroyed
saves data only for this instance of the activity during current session

17
Q

Restoring instance state

A

Two ways to retrieve the saved Bundle
in onCreate(Bundle mySavedState)
Preferred, to ensure that your user interface, including any saved state, is back up and running as quickly as possible
Implement callback (called after onStart())
onRestoreInstanceState(Bundle mySavedState)