Android Activity CS 4720 Mobile Application Development Source: - - PowerPoint PPT Presentation

android activity
SMART_READER_LITE
LIVE PREVIEW

Android Activity CS 4720 Mobile Application Development Source: - - PowerPoint PPT Presentation

Android Activity CS 4720 Mobile Application Development Source: developer.android.com CS 4720 Activity Conceptually, an Activity is a single screen of your application In other words, an App really is a collection of related


slide-1
SLIDE 1

CS 4720

Android Activity

CS 4720 – Mobile Application Development Source: developer.android.com

slide-2
SLIDE 2

CS 4720

Activity

  • Conceptually, an Activity is a single screen of

your application

  • In other words, an App really is a collection of

related Activities

  • Consider each Activity both a screen and a

feature

  • Apps can activate Activities in other Apps

2

slide-3
SLIDE 3

CS 4720

The App Lifecycle

3

slide-4
SLIDE 4

CS 4720

The Activity

4

slide-5
SLIDE 5

CS 4720

The Lifecycle

  • An Activity can be:

– Resumed – active and in the foreground with user focus – Paused – active, but is “covered up” by another activity that currently has the user focus – Stopped – currently residing in the background and is not attached to the window manager and is not visible; can be killed by system if memory is needed

5

slide-6
SLIDE 6

CS 4720

Lifecycle Callbacks

6

slide-7
SLIDE 7

CS 4720

Lifecycle Callbacks

  • Make sure to make the correct calls in the

correct methods!

  • If you do everything in onCreate(), then

problems could occur when you switch

  • On a device rotation, onDestroy() / onCreate()

is called

  • Consider saving state using
  • nSaveInstanceState() to retain App state

between switching

7

slide-8
SLIDE 8

CS 4720

  • nCreate()
  • Called when App is starting fresh
  • This is where setContentView() (setting the UI)

is called

  • This is why onCreate() is called on a rotation –

you may be using a different layout

8

slide-9
SLIDE 9

CS 4720

Next Steps

  • onStart() - Called right before Activity is shown

to user

  • onResume() – Called just before Activity will

start accepting user input

  • onPause() – Called when another Activity will

call onResume() – used to save remaining changes

  • onStop() – Called when Activity is no longer

visible

9

slide-10
SLIDE 10

CS 4720

UI

  • UI components come from the View class
  • A View controls a set of rectangular space and

can respond to user interaction

  • Can be the whole screen, or just a small

portion

  • Technically, a “widget” (like a button) is a very

small View

  • A Layout (and it’s xml) is a group of Views with

location instructions

10

slide-11
SLIDE 11

CS 4720

Adding to the Manifest

  • All Activities must be defined in the

androidmanifest.xml file

  • Intents that this Activity can respond to are

also defined here

11

slide-12
SLIDE 12

CS 4720

Launching a new Activity

  • To launch a new Activity, create an Intent for

that Activity and send it out

12

slide-13
SLIDE 13

CS 4720

Fragments

  • Consider a Fragment a sort of “sub-Activity”
  • An Activity can be made up of numerous

Fragments, each with their own features

  • This is useful when you want to move/reorder

components for different display sizes/rotations

  • A Fragment obeys the Activity’s lifecycle state

13

slide-14
SLIDE 14

CS 4720

Fragments

14

slide-15
SLIDE 15

CS 4720

Fragments

  • A Fragment has:

– Its own lifecycle calls – Its own layouts – Its own views

  • In fact, a Fragment looks almost exactly like an

Activity!

  • To add a Fragment, instantiate it in the layout
  • f the Activity

15

slide-16
SLIDE 16

CS 4720

FragmentManager

  • The main benefits Fragments are reusability

and addressability

  • Fragments can be accessed through the

FragmentManager and can be swapped on the fly while the App is running

  • Fragments can address the Activity that

contains them, allowing for sharing of data between Fragments

16