Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative...
Transcript of Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative...
![Page 1: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/1.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Android Developer FundamentalsThis work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Activities and Intents
1
Android Developer Fundamentals
Lesson 2
![Page 2: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/2.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
2.2 Activity Lifecycle and Managing State
2
![Page 3: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/3.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Contents
●Activity lifecycle●Activity lifecycle callbacks●Activity instance state●Saving and restoring activity state
3
![Page 4: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/4.jpg)
Android Developer Fundamentals
Activity Lifecycle
4
![Page 5: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/5.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
What is the Activity Lifecycle?
5
● The set of states an activity can be in during its lifetime, from when it is created until it is destroyed
More formally:● A directed graph of all the states an activity can
be in, and the callbacks associated with transitioning from each state to the next one
![Page 6: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/6.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
What is the Activity Lifecycle?
6
![Page 7: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/7.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Activity states and app visibility
7
● Created (not visible yet)● Started (visible)● Resume (visible)● Paused(partially invisible)● Stopped (hidden)● Destroyed (gone from memory)
State changes are triggered by user action, configuration changes such as device rotation, or system action
![Page 8: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/8.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Callbacks and when they are called
8
onCreate(Bundle savedInstanceState)—static initialization
onStart()—when activity (screen) is becoming visible
onRestart()—called if activity was stopped (calls onStart())
onResume()—start to interact with user
onPause()—about to resume PREVIOUS activity
onStop()—no longer visible, but still exists and all state info preserved
onDestroy()—final call before Android system destroys activity
![Page 9: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/9.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Activity states and callbacks graph
9
![Page 10: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/10.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Implementing and overriding callbacks
10
●Only onCreate() is required●Override the other callbacks to change default
behavior
![Page 11: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/11.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
●Called when the activity is first created, for example when user taps launcher icon
●Does all static setup: create views, bind data to lists, ...
●Only called once during an activity's lifetime●Takes a Bundle with activity's previously frozen
state, if there was one●Created state is always followed by onStart()
onCreate() –> Created
11
![Page 12: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/12.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onCreate(Bundle savedInstanceState)
12
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
}
![Page 13: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/13.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onStart() –> Started
13
● Called when the activity is becoming visible to user
● Can be called more than once during lifecycle
● Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden
![Page 14: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/14.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onStart()
14
@Override
protected void onStart() {
super.onStart();
// The activity is about to become
visible.
}
![Page 15: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/15.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onRestart() –> Started
15
● Called after activity has been stopped, immediately before it is started again
● Transient state
● Always followed by onStart()
![Page 16: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/16.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onRestart()
16
@Override
protected void onRestart() {
super.onRestart();
// The activity is between stopped and
started.
}
![Page 17: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/17.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onResume() –> Resumed/Running
17
● Called when activity will start interacting with user
● Activity has moved to top of the activity stack
● Starts accepting user input
● Running state
● Always followed by onPause()
![Page 18: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/18.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onResume()
18
@Override
protected void onResume() {
super.onResume();
// The activity has become visible
// it is now "resumed"
}
![Page 19: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/19.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onPause() –> Paused
19
● Called when system is about to resume a previous activity
● The activity is partly visible but user is leaving the activity
● Typically used to commit unsaved changes to persistent data, stop animations and anything that consumes resources
● Implementations must be fast because the next activity is not resumed until this method returns
● Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user
![Page 20: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/20.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onPause()
20
@Override
protected void onPause() {
super.onPause();
// Another activity is taking focus
// this activity is about to be "paused"
}
![Page 21: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/21.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onStop() –> Stopped
21
● Called when the activity is no longer visible to the user
● New activity is being started, an existing one is brought in front of this one, or this one is being destroyed
● Operations that were too heavy-weight for onPause
● Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away
![Page 22: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/22.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onStop()
22
@Override
protected void onStop() {
super.onStop();
// The activity is no longer visible
// it is now "stopped"
}
![Page 23: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/23.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onDestroy() –> Destroyed
23
● Final call before activity is destroyed
● User navigates back to previous activity, or configuration changes
● Activity is finishing or system is destroying it to save space
● Call isFinishing() method to check
● System may destroy activity without calling this, so use onPause() or onStop() to save data or state
![Page 24: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/24.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onDestroy()
24
@Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
}
![Page 25: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/25.jpg)
Android Developer Fundamentals
Activity Instance
State
25
![Page 26: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/26.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
When does config change?
26
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)
![Page 27: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/27.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
What happens on config change?
On configuration change, Android:
1. shuts down activity by calling:
27
● onPause()
● onStop()
● onDestroy()
● onCreate()
● onStart()
● onResume()
2. then starts it over by calling:
![Page 28: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/28.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Activity instance state
28
● State information is created while the activity is running, such as a counter, user text, animation progression
● State is lost when device is rotated, language changes, back-button is pressed, or the system clears memory
![Page 29: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/29.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Activity instance state
29
● System only saves:○ State of views with unique ID (android:id) such
as text entered into EditText○ Intent that started activity and data in its extras
● You are responsible for saving other activity and user progress data
![Page 30: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/30.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Saving instance state
30
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
![Page 31: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/31.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
onSaveInstanceState(Bundle outState)
31
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Add information for saving HelloToast counter
// to the to the outState bundle
outState.putString("count",
String.valueOf(mShowCount.getText()));
}
![Page 32: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/32.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Restoring instance state
32
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)
![Page 33: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/33.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
33
Restoring in onCreate()@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mShowCount = (TextView) findViewById(R.id.show_count);
if (savedInstanceState != null) {
String count = savedInstanceState.getString("count");
if (mShowCount != null)
mShowCount.setText(count);
}}
![Page 34: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/34.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
34
@Overridepublic void onRestoreInstanceState (Bundle mySavedState) { super.onRestoreInstanceState(mySavedState);
if (mySavedState != null) { String count = mySavedState.getString("count"); if (count != null) mShowCount.setText(count); }}
onRestoreInstanceState(Bundle state)
![Page 35: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/35.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Instance state and app restart
35
When you stop and restart a new app session, the activity instance states are lost and your activities will revert to their default appearance
If you need to save user data between app sessions, use shared preferences or a database.
![Page 36: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/36.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
Learn more
●Activity (API Guide)●Activity (API Reference)●Managing the Activity Lifecycle●Pausing and Resuming an Activity●Stopping and Restarting an Activity●Recreating an Activity●Handling Runtime Changes●Bundle
36
![Page 37: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/37.jpg)
Android Developer Fundamentals
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License
Activity Lifecycle & Managing
State
What's Next?
37
● Concept Chapter: 2.2 C Activity Lifecycle and Managing State
● Practical: 2.2 P Activity Lifecycle and Instance State
![Page 38: Activities and Intents · Android Developer Fundamentals This work is licensed under a Creative Commons Attribution-Non Commercial 4.0 International Lic ense Activity Lifecycle &](https://reader034.fdocuments.us/reader034/viewer/2022051805/5ff5c3f5cfa6a41f3d00f82f/html5/thumbnails/38.jpg)
Android Developer Fundamentals
END
38