Activity LifeCycle States of an Activity An activity has essentially four states If an activity in the foreground of the screen at the top of the stack it is active or running If an activity has lost focus but is still visible it is ID: 541346
Download Presentation The PPT/PDF document "Activity and Fragment" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Activity and FragmentSlide2
Activity
LifeCycleSlide3
States of an Activity
An activity has essentially four
states:If an activity in the foreground of the screen (at the top of the stack), it is active or running
.
If an activity has lost focus but is still visible it is
paused. A
paused activity is completely alive (it maintains all
state and
member information and remains attached to
the window
manager), but can be killed by the system
in
extreme
low memory situations
.
If an activity is completely obscured by another activity,
it is
stopped. It still retains all state and
member information
, however, it is no longer visible to the user
so its
window is hidden and it will often be killed by
the system
when memory is needed elsewhere
.
If an Activity is completely destroyed.Slide4
Specify Launcher Activity
<activity
android:name=".MainActivity" android:label="@string/app_name
">
<intent-filter>
<
action
android:name
="
android.intent.action.MAIN
" />
<
category
android:name
="
android.intent.category.LAUNCHER
" />
</intent-filter>
</activity
>
For activity to be default specify :
android:name
="
android.intent.category.DEFAULT
" Slide5
On Create
The system creates every new instance of Activity
by calling its onCreate() method
.
You should instantiate your class variables, declare the
UI for
the activity and
congure
the UI elements here.
TextView
mTextView
;
@Override
public void
onCreate
(Bundle
savedInstanceState
) {
super.onCreate
(
savedInstanceState
);
// Set the user interface layout for this Activity
setContentView
(
R.layout.main_activity
);
// Initialize member
TextView
mTextView
= (
TextView
)
findViewById
(
R.id.text_message
);
}Slide6
On Pause
The Activity is paused when the foreground activity
is sometimes obstructed by other visual components.
This
is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc.
@Override
public void
onPause
() {
super.onPause
(); // Always call the superclass method first
// Release the Camera because we don't need it when paused
// and other activities might need to use it.
if (
mCamera
!= null) {
mCamera.release
()
mCamera
= null;
}
}Slide7
On Resume
Called when focus comes back to the Activity
Should be used for acquiring back resources which
werereleased
on
onPause
() method and for setting
upanimations
, etc. which make sense when the user is interacting with the Activity
@Override
public void
onResume
() {
super.onResume
();
if (
mCamera
== null) {
// Local method to handle camera
init
initializeCamera
();
}
}Slide8
On Stop
The user opens the Recent Apps window and
switches from your app to another app. The activity in your app that's currently in the foreground is stopped. If the
user returns
to your app from the Home screen launcher icon
or the
Recent Apps window, the activity restarts
.
The user performs an action in your app that starts a
new activity. The current activity is stopped when the
second activity
is created. If the user then presses the
Back button
, the
rst
activity is restarted.
The user receives a phone call while using your app on
his
or
her phone.Slide9
Start/Restart Your Activity
When your activity comes back to the foreground from
the stopped state, it receives a call to onRestart()
The system also calls the
onStart
() method,
whichhappens
every time your activity becomes visible
Tip: Very uncommon for apps to use onRestart
()Slide10
Fragment
A Fragment represents a
behavior or a portion of user interface in an Activity
You can think of a fragment as a modular section of
an activity
, which has its own
lifecycle
Examples of Fragment are :
DialogFragment
,
ListFragment
,
PreferenceFragmentSlide11
3
Fragment
LifeCycleSlide12
Basic Fragment Code
public static class
ExampleFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater
inflater
,
ViewGroup
container,
Bundle savedInstanceState) { //
Inflate the layout for this fragment
return
inflater.inflate
(
R.layout.example_fragment
,
container
, false);
}
}Slide13
Questions ?