2. ?
What is started by the device
It contains the application's
information
Has method a to answer certain
events
An application could be composed
of multiple activities
5.
Resumed
The activity is in the foreground, and the user
can interact.
Paused
The activity is partially overlayed by another
activity. Cannot execute any code nor receive
inputs.
Stopped
Activity is hidden, in the background. It cannot
execute any code.
6. OnCreate()
Called when the activity is
created
Should contain the initialization
operations
Has a Bundle parameter
If onCreate() succesfull
terminates, it calls onStart()
7.
OnStart()
Called when onCreate() terminates
Called right before it is visible to
user
If it has the focus, then onResume()
is called
If not, onStop() is called
8. OnResume()
Called when the activity is
ready to get input from users
Called when the activity is
resumed too
If it succesfully terminates,
then the Activity is RUNNING
9.
OnPause()
Called when another activity comes
to the foreground, or when
someone presses back
Commit unsaved changes to
persistent data
Stop cpu-consuming processes
Make it fast
11. OnStop()
Activity is no longer visible to the
user
Could be called because:
the activity is about to be
destroyed
another activity comes to the
foreground
12. OnDestroy()
The activity is about to be destroyed
Could happen because:
The systems need some stack space
Someone called finish() method
on this activity
Could check with isFinishing()
13. Mainly 3 different loops
Entire lifetime
Between onCreate() and onDestroy().
Setup of global state in onCreate()
Release remaining resources in
onDestroy()
Visible lifetime
Between onStart() and onStop().
Maintain resources that has to be shown to
the user.
Foreground lifetime
Between onResume() and onPause().
Code should be light.
14. • Declare them before running them
• <activity android:name=".MainActivity"
android:label="@string/app_name">
• <intent-filter>
• <action
android:name="android.intent.action.MAIN"
/>
• <category
android:name="android.intent.category.LAUN
CHER" />
• </intent-filter>
• </activity>
In manifest file
15. • Open the MainActivity.java file in the
project, and override the
existing onStart method, which is called
when the activity is first viewed.
• Call the onStart method of the parent
class, and log a debug message:
• @Override
• public void onStart(){
super.onStart();
Log.d(CLASS_NAME, "onStart");
• }
16. • Override the existing onPause method,
which is called when another activity is
called to the foreground. Call
the onPause method of the parent and log
a debug message:
@Override
public void onPause(){
super.onPause();
Log.d(CLASS_NAME, "onPause");
}
• Override the existing onResume method, which is called
when the activity is running in the foreground and the user
can interact with it. Call the onResume method of the
parent class, and log a debug message:
• @Override public void onResume()
{ super.onResume();
• Log.d(CLASS_NAME, "onResume");
• }
17. Override the existing onStop method, which is called when the
activity is invisible to the end user. Call the onStop method of the
parent class, and log a debug message:
@Override public void onStop(){
super.onStop();
Log.d(CLASS_NAME, "onStop");
}
Override the existing onDestroy method, which is called when the
activity is removed from the system and can no longer be
interacted with. Call the onDestroy method of the parent class, and
log a debug message:
@Override public void onDestroy(){
super.onDestroy();
Log.d(CLASS_NAME, "onDestroy");
}
Override the existing onRestart method, which is called when the
activity is started again and returns to the foreground. Call
the onRestart method of the parent class and log a debug
message:
@Override public void onRestart(){
super.onRestart();
Log.d(CLASS_NAME, "onRestart"); }