Download presentation
Presentation is loading. Please wait.
1
Basic Activities and Intents
Landon Cox January 23, 2017
2
What is an Activity? The basics Activities have a three-part lifecycle
Java object “a single, focused thing that the user can do” Represents a screen for the user to interact with Entry point for the system and other code Activities have a three-part lifecycle Can be instantiated Can be visible Can be foregrounded
3
Launch Process killed Running Shut down onCreate() onStart()
onRestart() onResume() Process killed Running onPause() onStop() onDestroy() Shut down
4
Activity instantiated. Like a constructor. Init UI: setContenView.
Launch onCreate() onStart() Activity instantiated. Like a constructor. Init UI: setContenView. onResume() Activity made visible. Register non-user inputs, e.g., new messages Running Activity foregrounded. Start intensive UI widgets, e.g., camera live stream Starting points for three Entire, Visible, and Foreground lifetimes
5
Entire lifetime Launch Activity instantiated. Like a constructor.
onCreate() Activity instantiated. Like a constructor. Init UI: setContenView. Running Activity destroyed. Clean up before shutdown, e.g., save state to DB onDestroy() Shut down
6
Visible lifetime Launch Activity made visible.
onCreate() onStart() onRestart() Activity made visible. Register non-user inputs, e.g., new messages Running Activity no longer visible. Unregister non-user inputs, e.g., new messages onStop() Activity made visible. User returned to Activity. onDestroy() Shut down
7
Foreground lifetime Launch Activity foregrounded.
Start intensive UI widgets, e.g., camera live stream onCreate() onStart() onRestart() onResume() Running onPause() Activity backgrounded. Stop intensive UI widgets, e.g., camera live stream onStop() onDestroy() Shut down
8
Launch Process killed Running What’s this all about? Shut down
onCreate() onStart() onRestart() onResume() Process killed Running onPause() onStop() What’s this all about? onDestroy() Shut down
9
Activity lifecycle demo
10
How do we launch an Activity?
Android Intents An Intent is a message Each Intent has a destination (action) and payload (data) Intent action Describes the destination Activity (or other component) that should receive message Intent data Provides the receiver with extra information E.g., which URL to display or an address to message
11
What Activity should receive this message?
Launching ImageGrid What Activity should receive this message? Intent i = new Intent(MainActivity.this, ImageGridActivity.class); startActivity(i);
12
Is there any data associated with this Intent?
Launching ImageGrid Is there any data associated with this Intent? Intent i = new Intent(MainActivity.this, ImageGridActivity.class); startActivity(i);
13
This is an explicit Intent. We know exactly what Activity should run.
Launching ImageGrid This is an explicit Intent. We know exactly what Activity should run. Intent i = new Intent(MainActivity.this, ImageGridActivity.class); startActivity(i);
14
Launching ImageGrid Intent i = new Intent(MainActivity.this,
Can you think of an example when you want something done, but aren’t sure which code to run? Intent i = new Intent(MainActivity.this, ImageGridActivity.class); startActivity(i);
15
This is an implicit Intent.
Displaying a web URL This is an implicit Intent. We want a type of code to run, but don’t really care or know what code. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(" startActivity(browserIntent);
16
Let’s change our ImageViewer!
Displaying a web URL Let’s change our ImageViewer! Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(" startActivity(browserIntent);
17
Displaying a web URL Intent browserIntent =
This is still an implicit Intent. But we can set a preference for which code runs. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(" ")); browserIntent.setPackage("com.android.chrome"); startActivity(browserIntent);
18
Let’s update our ImageViewer!
Displaying a web URL Let’s update our ImageViewer! Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(" ")); browserIntent.setPackage("com.android.chrome"); startActivity(browserIntent);
19
How does Android know which Activities could receive this Intent?
Displaying a web URL How does Android know which Activities could receive this Intent? Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(" ")); startActivity(browserIntent);
20
Intent filters Restricts which actions the Activity will accept
(Intent Action must match) <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
21
Intent filters Describes categories of Intent Activity accepts
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> Describes categories of Intent Activity accepts (Intent categories must match all)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.