Understanding Android Activities and State Management

 
A
n
d
r
o
i
d
 
A
c
t
i
v
i
t
i
e
s
 
An application can have one or more activities, where
Each activity
Represents a screen that an app
    present to its user
Extends the 
Activity 
class
Activity's event handlers
onCreate(): when first created
onStart(): when visible
onResume(): when interactive
onPause(): when paused
onStop(): when no longer visible
onDestroy(): prior to destruction
onSaveInstanceState(Bundle)
 
 
 
A
c
t
i
v
i
t
y
 
S
t
a
t
e
s
 
The state of an activity depends on
Its position in the Activity stack
Activity states
Active
Activity at the top of the stack
Paused
Activity does not have focus
Stopped
Activity is not visible
Inactive
After an activity has been killed
 
 
 
 
 
 
E
v
e
n
t
 
H
a
n
d
l
e
r
s
 
t
o
 
M
o
n
i
t
o
r
 
S
t
a
t
e
C
h
a
n
g
e
s
 
Event handlers are defined to
Enable activities to react to state changes
Full lifetime:
Between onCreate and onDestroy
Visible lifetime:
Bound between onStart and onStop
Active lifetime
Starts with onResume and ends with onPause
 
 
 
 
 
A
p
p
l
y
i
n
g
 
T
h
e
m
e
s
 
t
o
 
a
n
 
A
c
t
i
v
i
t
y
 
By default,
An activity occupies the entire screen
 
However,
One can apply a dialog theme to an activity
This way, it displays as a floating dialog
 
How?
 
A
p
p
l
y
i
n
g
 
T
h
e
m
e
s
 
t
o
 
a
n
 
A
c
t
i
v
i
t
y
(
C
o
n
t
d
)
 
One can hide the title of an activity
By applying the following theme to your application:
@android:style/Theme.NoTitleBar
 
Refer to
ActivityAsDialog
 Android project
 
T
e
c
h
n
i
q
u
e
s
 
f
o
r
 
H
a
n
d
l
i
n
g
O
r
i
e
n
t
a
t
i
o
n
 
C
h
a
n
g
e
s
 
2 techniques are available
Anchoring
Anchor views to edges of the screen
 
Can be done by means of RelativeLayouts
 
Resizing and repositioning
Resize every view as a function of the current orientation
 
Refer to 
AlternativeLayouts
 Android project
 
A
n
c
h
o
r
i
n
g
v
i
e
w
s
 
RelativeLayout
Does the trick
 
On orientation change
Buttons remain
Anchored to screen edges
 
O
u
t
p
u
t
 
R
e
s
i
z
i
n
g
 
a
n
d
 
P
o
s
i
t
i
o
n
i
n
g
 
Idea
Create a different layout for each mode
 
By creating a new subfolder under 
res
Named 
layout-land
 
This way, there will be
main.xml
 contained in layout
Defining UI for portrait mode
 
main.xm
l in layout-land
Handling UI in landscape mode
 
E
x
a
m
p
l
e
:
 
l
a
y
o
u
t
 
E
x
a
m
p
l
e
:
 
l
a
y
o
u
t
-
l
a
n
d
 
O
u
t
p
u
t
 
D
e
t
e
c
t
i
n
g
 
O
r
i
e
n
t
a
t
i
o
n
 
C
h
a
n
g
e
s
 
Device’s current orientation
Can be detected during runtime as follows:
 
C
o
n
t
r
o
l
l
i
n
g
 
O
r
i
e
n
t
a
t
i
o
n
 
o
f
 
A
n
A
c
t
i
v
i
t
y
 
A change in orientation
Can be forced programmatically as follows
 
A
l
t
e
r
n
a
t
i
v
e
 
M
e
t
h
o
d
 
f
o
r
C
o
n
t
r
o
l
l
i
n
g
 
O
r
i
e
n
t
a
t
i
o
n
 
Alternatively, orientation can be forced as follows
 
I
m
p
l
i
c
a
t
i
o
n
 
o
f
 
O
r
i
e
n
t
a
t
i
o
n
C
h
a
n
g
e
s
 
Problem
Changing screen orientation destroys activity and recreates it
On recreation, current state of activity could be lost
 
=> Need to preserve the state of an activity
 
Fixes
Implement the 
onSaveInstance()
 method
Use 
SharedPreferences
 class
 
Refer to 
PreservingStateApp
 Android project
 
F
i
x
#
1
:
 
o
n
S
a
v
e
I
n
s
t
a
n
c
e
S
t
a
t
e
 
Idea
Preserve state and restore it later
Use the 
Bundle
 object to save current state:
 
 
 
Upon recreation, retrieve state saved previously:
 
 
 
 
Limitation
Not adequate for recovering complex data structures
 
F
i
x
#
2
:
 
U
s
i
n
g
 
S
h
a
r
e
d
P
r
e
f
e
r
e
n
c
e
s
 
SharedPrefernces
Points to a file containing key-value pairs
Providing simple methods to read and write them
By:
Getting a reference to a SharedPreference:
 
 
then, writing to a shared preference:
 
 
Then finally, reading data from shared perference
Context
 context 
=
 getActivity
();
SharedPreferences
 sharedPref 
=
 context
.
getSharedPreferences
(
        getString
(
R
.
string
.
preference_file_key
),
 
Context
.
MODE_PRIVATE
);
SharedPreferences
 sharedPref 
=
 getActivity
().
getPreferences
(
Context
.
MODE_PRIVATE
);
SharedPreferences
.
Editor
 editor 
=
 sharedPref
.
edit
();
editor
.
putInt
(
getString
(
R
.
string
.
saved_high_score
),
 newHighScore
);
editor
.
commit
();
SharedPreferences
 sharedPref 
=
 getActivity
().
getPreferences
(
Context
.
MODE_PRIVATE
);
int
 defaultValue 
=
 getResources
().
getInteger
(
R
.
string
.
saved_high_score_default
);
long
 highScore 
=
 sharedPref
.
getInt
(
getString
(
R
.
string
.
saved_high_score
),
 defaultValue
);
Slide Note
Embed
Share

An Android application consists of activities, each representing a screen for user interaction. Activities have states like active, paused, and stopped. Event handlers and themes play crucial roles in monitoring state changes and applying visual styles to activities. Techniques for handling orientation changes involve anchoring views to the screen edges and resizing them based on orientation.


Uploaded on Jul 26, 2024 | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Android Activities An application can have one or more activities, where Each activity Represents a screen that an app present to its user Extends the Activity class Activity's event handlers onCreate(): when first created onStart(): when visible onResume(): when interactive onPause(): when paused onStop(): when no longer visible onDestroy(): prior to destruction onSaveInstanceState(Bundle)

  2. Activity States The state of an activity depends on Its position in the Activity stack Activity states Active Activity at the top of the stack Paused Activity does not have focus Stopped Activity is not visible Inactive After an activity has been killed

  3. Event Handlers to Monitor State Changes Event handlers are defined to Enable activities to react to state changes Full lifetime: Between onCreate and onDestroy Visible lifetime: Bound between onStart and onStop Active lifetime Starts with onResume and ends with onPause

  4. Applying Themes to an Activity By default, An activity occupies the entire screen However, One can apply a dialog theme to an activity This way, it displays as a floating dialog How?

  5. Applying Themes to an Activity (Cont d) One can hide the title of an activity By applying the following theme to your application: @android:style/Theme.NoTitleBar Refer to ActivityAsDialog Android project

  6. Techniques for Handling Orientation Changes 2 techniques are available Anchoring Anchor views to edges of the screen Can be done by means of RelativeLayouts Resizing and repositioning Resize every view as a function of the current orientation Refer to AlternativeLayouts Android project

  7. Anchoring views RelativeLayout Does the trick On orientation change Buttons remain Anchored to screen edges

  8. Output

  9. Resizing and Positioning Idea Create a different layout for each mode By creating a new subfolder under res Named layout-land This way, there will be main.xml contained in layout Defining UI for portrait mode main.xml in layout-land Handling UI in landscape mode

  10. Example: layout

  11. Example: layout-land

  12. Output

  13. Detecting Orientation Changes Device s current orientation Can be detected during runtime as follows:

  14. Controlling Orientation of An Activity A change in orientation Can be forced programmatically as follows

  15. Alternative Method for Controlling Orientation Alternatively, orientation can be forced as follows

  16. Implication of Orientation Changes Problem Changing screen orientation destroys activity and recreates it On recreation, current state of activity could be lost => Need to preserve the state of an activity Fixes Implement the onSaveInstance() method Use SharedPreferences class Refer to PreservingStateApp Android project

  17. Fix#1: onSaveInstanceState Idea Preserve state and restore it later Use the Bundle object to save current state: Upon recreation, retrieve state saved previously: Limitation Not adequate for recovering complex data structures

  18. Fix#2: Using SharedPreferences SharedPrefernces Points to a file containing key-value pairs Providing simple methods to read and write them By: Getting a reference to a SharedPreference: Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences( getString(R.string.preference_file_key), Context.MODE_PRIVATE); then, writing to a shared preference: SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.saved_high_score), newHighScore); editor.commit(); Then finally, reading data from shared perference SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); int defaultValue = getResources().getInteger(R.string.saved_high_score_default); long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#