lecture #4 activities & fragments

107
Campus-Guest Techiteasy

Upload: vitali-pekelis

Post on 12-Apr-2017

98 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Lecture #4  activities & fragments

Campus-GuestTechiteasy

Page 2: Lecture #4  activities & fragments

Yarkoni

IronSource

Android AcademyWomen Techmakers

Page 3: Lecture #4  activities & fragments

~ 2000 members Largest Android Active Community

Page 4: Lecture #4  activities & fragments

What Do We Do?

●Android Fundamentals

●Android UI / UX

●Community Hackathon

●Android Performance

●Mentors Program●Active community

Page 5: Lecture #4  activities & fragments

Online Lessons

Important:

Watch online lesson

before the meetup!

- Our course: “Developing

Android Apps”goo.gl/u1pxZv

- Optional: Nano Degree

- Optional: “Android Basics” courses

Page 6: Lecture #4  activities & fragments
Page 7: Lecture #4  activities & fragments

The Course Plan

- Online lesson @ home - Lecture @ Campus- Hands-on @ Campus- Questions @ Facebook

Page 8: Lecture #4  activities & fragments

YarkoniAndroid Leader

Ironsource

Android Academy Staff

Yonatan LevinGoogle Developer

Expert & Android @ Gett

Britt BarakAndroid Leader

Figure8

Yossi SegevAndroid Developer

Crave

Page 9: Lecture #4  activities & fragments

Mentors program

Page 10: Lecture #4  activities & fragments

Community Mentors

Limor Mekaiten

Page 11: Lecture #4  activities & fragments

Lecture #4: Activities, Fragments &

LifecyclesKnowing where you are is

important

Page 12: Lecture #4  activities & fragments

What’s For Today?● Activity● Fragment● Persisting data● External libraries

Page 13: Lecture #4  activities & fragments

Reminder - Listeners / callbacks

In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time.Function a

(listener)Function a (listener)

Time

Function a (listener)

Function a (listener)

Trigger

Page 14: Lecture #4  activities & fragments

Example - callback interface

Page 15: Lecture #4  activities & fragments

Example - CREATE & use callback interface

Page 16: Lecture #4  activities & fragments

Activity

Page 17: Lecture #4  activities & fragments
Page 18: Lecture #4  activities & fragments

Activity - Creation

Java file.AndroidManifest.xml declaration.

Page 19: Lecture #4  activities & fragments

Android Studio - Quick tip

Page 20: Lecture #4  activities & fragments

Entry Point Java Program?

Page 21: Lecture #4  activities & fragments

Entry Point Java Program?public static void main(String[] args)

Page 22: Lecture #4  activities & fragments

Entry Point Android?

Page 23: Lecture #4  activities & fragments

Entry Point Android?“Activity Lifecycle”

Page 24: Lecture #4  activities & fragments

Do not try this at home(It won’t work)

Page 25: Lecture #4  activities & fragments

A way to think of the callbacks

Page 26: Lecture #4  activities & fragments

Activity Lifecycle

Lifecycle is a sequence of setup and teardown callbacks.

Application lifecycle != Thread lifecycle != activity lifecycle.

User and System determine lifecycle.

Page 27: Lecture #4  activities & fragments

Activity Lifecycle

An application normally has several activities.When a new activity starts the old one goes into the

stack (LIFO).3 States:

Resumed - foreground has user focus

Paused - partially visible (in memory)

Stopped - not visible (in memory but can be killed)Activity a

Activity b

Activity c

Page 28: Lecture #4  activities & fragments

If BACK button pops from the Stack

What does the HOME button do?

Page 29: Lecture #4  activities & fragments

If BACK button pops from the Stack

What does the HOME button do?

onUserLeaveHint()

Page 30: Lecture #4  activities & fragments

Activity lifecycle - callbacks

onCreateonResumeonStartonPauseonStoponDestory

Page 31: Lecture #4  activities & fragments

Activity life cycle

Determined by the user and the system.

Page 32: Lecture #4  activities & fragments

Activity Lifecycle - Corresponding Methods

Entire lifetime.Visible lifetime.Foreground lifetime.

Page 33: Lecture #4  activities & fragments

What to do in every state

onCreate & onDestroy will be called at most once.

onPause & onResume can be called a lot.

Foreground

Visible

Background

onPause()

onStop()

onResume()

onStart()

Page 34: Lecture #4  activities & fragments

Activity - onCreate()

When: on first creation of activity.What: inflate view, find references, bind data,

initialize one timers.Also has bundle with previous frozen state.

Page 35: Lecture #4  activities & fragments

Activity - onStart()

●When: the activity is becoming visible to the user.●What: specific stuff which happens every time.●Called every time.

Page 36: Lecture #4  activities & fragments

Activity - onResume()

●When: User interaction.●What: resume drawing start animations add

listeners.●Top of the activity stack.●Always followed by onPause().

Page 37: Lecture #4  activities & fragments

Activity - onPause()

●When: resuming a previous activity●What: commit unsaved changes to persistent data,

stop intensive CPU actions but keep drawing, remove listeners.

●Promised by the system.

Page 38: Lecture #4  activities & fragments

Activity - onStop()● When: Activity is no longer visible either from

starting a new one or destroying the current one.●What: stop drawing and animations. ●onRestart() may be triggered after onStop().

Page 39: Lecture #4  activities & fragments

Activity - onDestroy()● When: final call before dying.●What: Nothing important.●Not promised.●Can be called due to explicit or implicit

destruction. isFinishing() allows us to distinguish.

Page 40: Lecture #4  activities & fragments

Example - App Starts for the First Time

Page 41: Lecture #4  activities & fragments

Example 2- Phone Call

Page 42: Lecture #4  activities & fragments

Example 3 - ?

Page 43: Lecture #4  activities & fragments

Example 3 - Configuration Change (rotate)

Page 44: Lecture #4  activities & fragments

Activity - Life and death●When is activity “killable”?

○onStop

○OnDestroy

●Where do we save the data?○onPause

●Calling finish() goes directly to onDestroy()

Page 45: Lecture #4  activities & fragments

Activity - Rotation● A.K.A configuration change.●Destroyed and re-created.

○re-retrieve all resources, drawables, layouts and strings.

●Requires declaration in manifest per activity and @Override onConfigurationChange() method.

Page 46: Lecture #4  activities & fragments

AndroidManifest.xml + SecondActivity.java

Page 47: Lecture #4  activities & fragments

Developer Responsibilities● Be aware of lifecycle.

● Unregister/Stop actions before death.

● Save state before death.

Page 48: Lecture #4  activities & fragments

Developer Responsibilities Example

Does not crash if the user receives a phone call or switches to another app.

Does not consume valuable system resources when the user is not actively using your app.

Does not lose user’s progress when he leaves and returns to the app.

Does not crash or lose user’s progress on orientation change.

Page 49: Lecture #4  activities & fragments

Saving Persistent State

Page 50: Lecture #4  activities & fragments

Saving Persistent Data

Shared document-like data (SQL / Contentprovider).User preference (savedInstance / SharedPrefs).

Page 51: Lecture #4  activities & fragments

Activity - Data Models● Primitives using <Key, Value> pairs.●@Serializable/@Parcelable.

Page 52: Lecture #4  activities & fragments

Saving Persistent DataSharedPrefernces

Example

Page 53: Lecture #4  activities & fragments

Get & Set SharedPrefs

Page 54: Lecture #4  activities & fragments

Saving Persistent DataSavedInstance Primitive

Example

Page 55: Lecture #4  activities & fragments

On the way out

Page 56: Lecture #4  activities & fragments

On the way in

Page 58: Lecture #4  activities & fragments

Saving Persistent DataSavedInstance Parcelable

Example

Page 59: Lecture #4  activities & fragments

Dedicated pojo

Page 60: Lecture #4  activities & fragments

Dedicated pojo

Page 61: Lecture #4  activities & fragments

Parcelable - How to useMainActivity.java

Page 62: Lecture #4  activities & fragments

Parcelable - How to useSecondActivity.java

Page 63: Lecture #4  activities & fragments

Shortcut!

Page 64: Lecture #4  activities & fragments

SavedInstanceState - IcePick

Icepick - https://github.com/frankiesardo/icepickAnnotation based.

Page 65: Lecture #4  activities & fragments
Page 66: Lecture #4  activities & fragments

Everything has a Price

Page 67: Lecture #4  activities & fragments

SavedInstanceState - TEST it!

Don’t keep activities.

Page 68: Lecture #4  activities & fragments

Fragments

Page 69: Lecture #4  activities & fragments

Represents an operation or interface within an Activity

Page 70: Lecture #4  activities & fragments

Fragments

●Started with android API 11 (Honeycomb).●Sophisticated UI on larger screens.●Modularize code.

○ Receives its own input.

○ Can be added or removed while the activity is running.

○ Has its own lifecycle.

Page 71: Lecture #4  activities & fragments

Fragments - Better UI

Page 72: Lecture #4  activities & fragments

Fragments - Code Recipe

Must have empty constructor.Use factory design pattern.onCreate receives arguments passed.onCreateView return inflated View.

Page 73: Lecture #4  activities & fragments

Fragment - onCreate()

When: creating the fragment.What: Initialize essential component of the fragment

that you want to retain when the fragment is paused or stopped, then resumed.

Page 74: Lecture #4  activities & fragments

Fragment - onCreateView()

●When: drawing occurs for the first time.●What: To draw a UI for a fragment, you must return

a view from this method that is the root of your fragment’s layout. You can return null if the fragment does not provide a UI.

Page 75: Lecture #4  activities & fragments

Fragment - Lifecycle

Page 76: Lecture #4  activities & fragments

Fragment - Activity Lifecycle relation

●Important - Activity drives the fragment.

Page 77: Lecture #4  activities & fragments

Fragments1st way

Page 78: Lecture #4  activities & fragments

Fragment creation using Factory

Page 79: Lecture #4  activities & fragments

Fragment creation using Factory

Page 80: Lecture #4  activities & fragments
Page 81: Lecture #4  activities & fragments

Fragments2nd way

Page 82: Lecture #4  activities & fragments

Fragments - FragmentManager

Interaction with fragments is done via FragmentManager.

Has its own back stack for transactions - addToBackStack().

Page 83: Lecture #4  activities & fragments
Page 84: Lecture #4  activities & fragments

Fragments - Backstack

●When a user presses backIn an activity any Transaction on the BackStack are poppedfff before the activity finishes itself. Fragment a

Fragment b

Fragment c

Fragment BackStack

Activity B

Activity A

Act

iviti

es B

ackS

tack

Page 85: Lecture #4  activities & fragments

FragmentsDifferent UI for Tablets

Page 86: Lecture #4  activities & fragments
Page 87: Lecture #4  activities & fragments
Page 88: Lecture #4  activities & fragments

Fragments - Support Multiple Screens

Smallest width.

Page 89: Lecture #4  activities & fragments

Fragment - Passing data between fragments

●Should always occur via the joint parent activity.

Page 90: Lecture #4  activities & fragments

Fragments - Passing data between fragments

●Should always occur via the joint parent activity.●Define an interface in the fragment class and

implement it within the activity.●The Fragment captures the interface

implementation in onAttach().●From there you can call interface methods to

communicate to the activity.●To communicate down

getSupportFragmentManager(). findFragmentById().

Page 91: Lecture #4  activities & fragments

Activity implements Fragment’s interface

Fragments - Passing data between fragments

Fragment A has reference to Activity

Fragment B has reference to Activity

Page 92: Lecture #4  activities & fragments

Fragment Callback to activity

Page 93: Lecture #4  activities & fragments

Activity Implementation

Page 94: Lecture #4  activities & fragments

Activity

Page 95: Lecture #4  activities & fragments

Shortcut!

Page 96: Lecture #4  activities & fragments

What is the connection?

Page 97: Lecture #4  activities & fragments

Fragments - Passing data between fragments

Otto LibraryEvent bus.

Decouples code.

Communication.

Page 98: Lecture #4  activities & fragments

Otto - Recipe

Create a singleton holding an instance of Bus class.Register to bus.Create a method to receive and annotate with

@Subscribe.Publish message to subscribers.Unregister to bus (onStop).

Page 99: Lecture #4  activities & fragments

Singleton making Bus accessible to all

Page 100: Lecture #4  activities & fragments

Activity - Register to bus

Page 101: Lecture #4  activities & fragments

Method to Receive the message

Page 102: Lecture #4  activities & fragments

Publish message to Subscribers

Page 103: Lecture #4  activities & fragments

DO NOT forget to unregister

Page 104: Lecture #4  activities & fragments

Homework:1.Go over Activity &

Fragments.2.Watch Lesson #5 of Udacity.

3.Mid stage Sunshine development.

Page 105: Lecture #4  activities & fragments

34th floor

Page 106: Lecture #4  activities & fragments
Page 107: Lecture #4  activities & fragments

Drive home safe

See you next Sunday!