ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method:...

Preview:

Citation preview

ACTIVITY AND FRAGMENTS

Introduction • An application is composed of at least

one Activity

• It is a software component that stays behind a GUI (screen)

• It runs inside the ‘main’ thread and it should be reactive to user’s input

• Managed by the ‘Activity Manager’

Activity

GUI

How an activity is created?• An Activity can be created by the ‘Launcher’

• The corresponding icon on the home screnn is pushed• It is the starting point of an application

• An activity can also be created by another activity or othersw components (see later)

• An activity declares to the system its ability to perform‘actions’ through an intent-filter (declared in the manifest)

• The system will activate the activity when some othercomponent in the system needs to perform the action

How an Activity is created?

ActivityActivity

Manager

register with am

callb

ack

met

hods

ActivityManager

Activity Activitystart(…)

Action?

GUlevents

Sw hierarchy

java.lang.Object

android.content.context

android.content.contextWrapper

android.view.contextThemeWrapper

android.app.Activity

Interface to global information about an application environment. Implemented by ‘android’.Lots of methods (see documentation)

See: https://www.artima.com/insidejvm/ed2/threadsynchP.html

Delegate implementation

Allows to interact with theme

Activity permissions and features• An activity (or other SW components of an application)

may require the permission to use some resources of the device

• Permission are listed in the manifest file and granted atinstallation time

• Starting with android 6.0, permission are also granted atexecution time

• An application may also require thre presence of samefeature for being executed (e.g., a camera)

Process, Task, Thread• Each application runs inside its own Process• Usually, other threads are created to peform long running

operations• The set of activities lunched by a user is a Task• Simple navigation across activities is obtained through a

LIFO back stack

Back stack• Activities in the system are managed via an back stack• When a new activity is created, it is placed on the top of

the stack and becomes active • The previous activity remains below in the stack until the

running activity is deleted (for example back button)

Ak

A2

A1

Running activity

Back stack

States of an activity • Running: the activity is in foreground and has the focus• The activity is visible and it has the focus, i.e., all events

are delivered to it

• Pause: it is partially visible, but lost the focus • For example the foreground activity doesn’t occupy the whole

screen• A paused activity maintains all state and member information, but

the system can kill it

• Stopped: the activity is completely obscured by another one

Life cycle states

• In the Paused or Stopped state an activity can be killed by the am when resources are needed to the OS.

Lifecycle methods • Each activity in an application

goes through its own lifecycle.

• Once and only once when an activity is created, is the onCreate() function executed.

• If the activity exits, the onDestroy() function is executed.

• In between, various events can lead to the activity being in multiple different states…

ActivityLifecycle (demo)

I/INFO﹕ A: onCreateI/INFO﹕ A: onStartI/INFO﹕ A: onResume (Button is clicked)

I/INFO﹕ A: onPauseI/INFO﹕ B: onCreateI/INFO﹕ B: onStartI/INFO﹕ B: onResumeI/INFO﹕ A: onSaveInstanceState (now the activity can be killed…)

(back botton is pressed)

I/INFO﹕ B: onPauseI/INFO﹕ A: onResumeI/INFO﹕ B: onStopI/INFO﹕ B: onDestroy

note method calls are interleaved

android:theme="@android:style/Theme.Dialog"

ActivityLifeCycle (demo)

• I/INFO﹕A: onCreate• I/INFO﹕A: onStart• I/INFO﹕A: onResume (second acivity is lunched an takes all the screen)

• I/INFO﹕A: onPause• I/INFO﹕ B: onCreate• I/INFO﹕ B: onStart• I/INFO﹕ B: onResume• I/INFO﹕A: onSaveInstanceState• I/INFO﹕A: onStop (First activity is no longer visible)

• I/INFO﹕ B: onPause (back button is now pressed)• I/INFO ﹕A: onRestart• I/INFO ﹕A: onStart• I/INFO﹕A: onResume• I/INFO﹕ B: onStop• I/INFO﹕ B: onDestroy

ActivityLifeCycle (demo)• Changing device’s orientation• ….

• /INFO﹕ A: onPause• I/INFO﹕A: onSaveInstanceState• I/INFO﹕A: onStop• I/INFO﹕A: onDestroy

• I/INFO﹕A: onCreate• I/INFO﹕A: onStart• I/INFO﹕A: onRestoreInstanceState (this method is called when the activity is created again)• I/INFO﹕A: onResume

Lifecycle methods• Method: onCreate(Bundle savedInstanceState)

• Called when the activity is first created• This is where normal static initialization should go, e.g.,

create views, bind data to list, etc. • This method is passed a Bundle object containing the

activity’s previous state (null the first time). • Always followed by onStart()

Lifecycle methods• Method: onStart()• Called just before the activity becomes visible to the user• It is called also when the activity is becoming visible again• Put additional initialization code

• Method: onResume()

• Called just before the activity starts interacting with the user.

• At this point the activity is at the top of the activity stack, with user input going to it.

• Here for example one can starts other threads…

Lifecycle methods• Method: onRestart()• Called after the activity has been stopped, just prior to it

being started again.

Lifecycle methods• Method: onPause()

• Called when the system is about to start resuming another activity.

• This method is typically used to commit unsaved changes to persistent data, stop animations, threads, and any thing that may be consuming CPU, and so on.

• It should do whatever it does very quickly, because the next activity will not be resumed until it returns.

• Followed either by onResume() if the activity returns back to the front, or by onStop() if it becomes invisible to the user.

• After this method, the activity is killable by the system.

Lifecycle methods• Method: onStop()

• Called when the activity is no longer visible to the user.

• This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.

• Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.

Lifecycle methods• Method: onDestroy()

• Called before the activity is destroyed.

• This is the final call that the activity will receive.

• It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space.

• One can distinguish between these two scenarios with the isFinishing() method.

Should you implement all methods?• All of these methods are hooks that one can override to

do appropriate work when the state changes.

• All activities must implement onCreate() to do the initial setup when the object is first instantiated.

• It will also important (recommended) to implement onPause() to commit data changes and otherwise prepare to stop interacting with the user.

Saving the state• Before to be killable, the onSaveInstanceState(Bundle

savedInstanceState) is called• Here one can store specific activity’s state variables (in

the bundle)• The Bundle is passed to onCreate method and to

onRestoreInstanceState method

ActivityManager (demo)

Fragments• Useful for large screen (like tablet)• or as navigation tools• Fragments are hosted inside an Activity

• A fragment is ‘attached’ to a View• It must creates its view (from an XML file)

• Fragments have their own lifecycle (which is more complex than the activity)

• Can be added via XML file or programmatically

Fragments and Activities

CVFragment A

CVFragment B

Activity

Activity’slifecycle methods

Fragment specificlifecycle methods

FragmentManager

getActivity()

Fragment lifecycle

Sw hierarchy

java.lang.Object

android.app.Fragment

Using Fragments

FragmentXML (demo)

Two fragmentsOne is ListFragmentActivity is the glue among them

Warning: onAttach method changed in API 23!

FragmentProgram (demo)

Wizard from android studio• Activity with one fragment• Builds the skeleton of an application with a list drawer and

a main fragment. It also has an overflow button and actionbutton

Recommended