activity and fragments - uniroma1.itberaldi/macc_16/slides/04.pdf · lifecycle methods • method:...

32
ACTIVITY AND FRAGMENTS

Upload: others

Post on 29-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

ACTIVITY AND FRAGMENTS

Page 2: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

Page 3: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

Page 4: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

How an Activity is created?

ActivityActivity

Manager

register with am

callb

ack

met

hods

ActivityManager

Activity Activitystart(…)

Action?

GUlevents

Page 5: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

Page 6: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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)

Page 7: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

Page 8: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

Page 9: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

Back stack

Page 10: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

Page 11: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

Life cycle states

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

Page 12: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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…

Page 13: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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"

Page 14: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

Page 15: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

Page 16: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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()

Page 17: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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…

Page 18: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

being started again.

Page 19: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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.

Page 20: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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.

Page 21: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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.

Page 22: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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.

Page 23: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

Page 24: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

ActivityManager (demo)

Page 25: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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

Page 26: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

Fragments and Activities

CVFragment A

CVFragment B

Activity

Activity’slifecycle methods

Fragment specificlifecycle methods

FragmentManager

getActivity()

Page 27: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

Fragment lifecycle

Page 28: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

Sw hierarchy

java.lang.Object

android.app.Fragment

Page 29: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

Using Fragments

Page 30: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

FragmentXML (demo)

Two fragmentsOne is ListFragmentActivity is the glue among them

Warning: onAttach method changed in API 23!

Page 31: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

FragmentProgram (demo)

Page 32: ACTIVITY AND FRAGMENTS - uniroma1.itberaldi/MACC_16/slides/04.pdf · Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It

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