ACTIVITY AND FRAGMENTS Introduction An application is composed of - - PowerPoint PPT Presentation

activity and fragments introduction
SMART_READER_LITE
LIVE PREVIEW

ACTIVITY AND FRAGMENTS Introduction An application is composed of - - PowerPoint PPT Presentation

ACTIVITY AND FRAGMENTS Introduction An application is composed of at least one Activity GUI It is a software component that stays behind a GUI (screen) Activity It runs inside the main thread and it should be reactive to


slide-1
SLIDE 1

ACTIVITY AND FRAGMENTS

slide-2
SLIDE 2

Introduction

  • An application is composed of at least
  • ne 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

slide-3
SLIDE 3

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 other

sw 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 other

component in the system needs to perform the action

slide-4
SLIDE 4

How an Activity is created?

Activity Activity Manager

register with am

callback methods

Activity Manager Activity Activity start(…) Action? GUl events

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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 at

installation time

  • Starting with android 6.0, permission are also granted at

execution time

  • An application may also require thre presence of same

feature for being executed (e.g., a camera)

slide-7
SLIDE 7

Process, Task, Thread

  • Each application runs inside its own Process
  • Usually, other threads are created to peform long running
  • perations
  • The set of activities lunched by a user is a Task
  • Simple navigation across activities is obtained through a

LIFO back stack

slide-8
SLIDE 8

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

slide-9
SLIDE 9

Back stack

slide-10
SLIDE 10

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
  • ne
slide-11
SLIDE 11

Life cycle states

  • In the Paused or Stopped

state an activity can be killed by the am when resources are needed to the OS.

slide-12
SLIDE 12

Lifecycle methods

  • Each activity in an application

goes through its own lifecycle.

  • Once and only once when an

activity is created, is the

  • nCreate() function executed.
  • If the activity exits, the
  • nDestroy() function is executed.
  • In between, various events can

lead to the activity being in multiple different states…

slide-13
SLIDE 13

ActivityLifecycle (demo)

I/INFO﹕ A: onCreate I/INFO﹕ A: onStart I/INFO﹕ A: onResume (Button is clicked) I/INFO﹕ A: onPause I/INFO﹕ B: onCreate I/INFO﹕ B: onStart I/INFO﹕ B: onResume I/INFO﹕ A: onSaveInstanceState (now the activity can be killed…) (back botton is pressed)

I/INFO﹕ B: onPause I/INFO﹕ A: onResume I/INFO﹕ B: onStop I/INFO﹕ B: onDestroy

note method calls are interleaved

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

slide-14
SLIDE 14

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
slide-15
SLIDE 15

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
slide-16
SLIDE 16

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()
slide-17
SLIDE 17

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…
slide-18
SLIDE 18

Lifecycle methods

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

being started again.

slide-19
SLIDE 19

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,
  • r by onStop() if it becomes invisible to the user.
  • After this method, the activity is killable by the system.
slide-20
SLIDE 20

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

  • ne) 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.

slide-21
SLIDE 21

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.

slide-22
SLIDE 22

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
  • nPause() to commit data changes and otherwise prepare

to stop interacting with the user.

slide-23
SLIDE 23

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
  • nRestoreInstanceState method
slide-24
SLIDE 24

ActivityManager (demo)

slide-25
SLIDE 25

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
slide-26
SLIDE 26

Fragments and Activities

CV Fragment A CV Fragment B Activity Activity’s lifecycle methods Fragment specific lifecycle methods Fragment Manager

getActivity()

slide-27
SLIDE 27

Fragment lifecycle

slide-28
SLIDE 28

Sw hierarchy

java.lang.Object android.app.Fragment

slide-29
SLIDE 29

Using Fragments

slide-30
SLIDE 30

FragmentXML (demo)

Two fragments One is ListFragment Activity is the glue among them Warning: onAttach method changed in API 23!

slide-31
SLIDE 31

FragmentProgram (demo)

slide-32
SLIDE 32

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 action button