chapter 12 - activity intent

24
Ac#vity & Intent

Upload: sittiphol-phanvilai

Post on 11-Apr-2017

90 views

Category:

Education


0 download

TRANSCRIPT

Ac#vity & Intent

[Recap] What is Ac#vity?

Ac$vity Ac$vity class takes care of crea$ng a

window for you in which you can place your UI

Introduc#on to Ac#vity

• Ac$vity is one of the core and key system in Android • Very important, you can’t just

know about it, you have to MASTER at it • Key message to fully understand: •  Lifecycle

Play around: Hide The Title Bar

1)  Hide by Code 2)  Hide by Style

Why do we hide it?

Live Demo Custom Title Bar No Title Bar at all

Play around: Styles & Theme

Naviga#on: The second Ac#vity

1)  Create New Ac$vity 2)  Try to startActivity 3)  See what happened? 4)  Play around with AndroidManifest.xml 5)  Now try again

Live Demo

Intent

An Intent is a messaging object you can use to request an ac$on from another app component.

Live Demo

?

Lab: Intent & Bundle

Q: Why we need to send data through Intent? A: Because Ac)vity is independent to each other

… Play Play Play …

Understand how Android OS works

•  Secret: Actually Launcher (Home Screen) is also an Ac$vity ! • And launcher also launch

our applica$on through Intent and startActivity

Live Demo

Intent Filter

<activity

android:name="com.example.helloworld.MainActivity"

android:theme="@style/AppTheme"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

Take a look into AndroidManifest.xml

What happened when app launched? Tasks and Back Stack

Live Demo

The Ac#vity Lifecycle

IMPORTANT ALERT

The Ac#vity Lifecycle #2

What do we do in each lifecycle event in prac#cal?

onCreate

Ini$alize UI Elements

onResume or onStart

Ini$alize System Resources (GPS,

Camera, etc)

onDestroy

Mostly do nothing

onPause or onStop

Release System Resources (GPS,

Camera, etc)

Stop CPU consuming task like Anima$on,

Timer

Commit unsaved changes

Pause Game

Why or?

Save & Restore Instance State

Live Demo

You will a liOle bit confuse now

But it will be be^er when you prac$ce more

(and more) (and more) (and more)

Best Prac#ces: Ac#vity Code Structure

•  In The Android Cheese Sheet

How to return from Ac#vity

Live Demo

finish()

FirstAc)vity

startActivity(...)

SecondAc)vity

GeRng a Result from an Ac#vity

Live Demo

setResult(...);

finish()

FirstAc)vity

startActivityForResult(...)

SecondAc)vity

The Screen Orienta#on

android:screenOrientation=["unspecified" | "behind" |

"landscape" | "portrait" |

"reverseLandscape" | "reversePortrait" |

"sensorLandscape" | "sensorPortrait" |

"userLandscape" | "userPortrait" |

"sensor" | "fullSensor" | "nosensor" |

"user" | "fullUser" | "locked"]

Live Demo

You can fix the Ac$vity’s orienta$on in AndroidManifest.xml

Sugges$on: Fix Orienta$on to portrait at the beginning

(why?)

SoS Keyboard Mode

android:windowSoftInputMode=["stateUnspecified",

"stateUnchanged",

"stateHidden",

"stateAlwaysHidden",

"stateVisible",

"stateAlwaysVisible",

"adjustUnspecified",

"adjustResize",

"adjustPan"]

Live Demo

You can change behavior of sod keyboard in Ac$vity through AndroidManifest.xml

Ac#vity Transi#on

• Basically transi$on will be done by default seengs • But you can override it (but not so recommend unless it is by design) •  Example: <?xml version="1.0" encoding="utf-8"?>

<alpha xmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@android:anim/accelerate_interpolator"

android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" /> res/anim/fade_in.xml

<?xml version="1.0" encoding="utf-8"?>

<alpha xmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@android:anim/accelerate_interpolator"

android:fromAlpha=“1.0" android:toAlpha=“0.0" android:duration="2000" /> res/anim/fade_out.xml

overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

Recap

•  Every$me you create a new Ac$vity, declare in AndroidManifest.xml immediately • Ac)vity Lifecycle is very very very important •  In can effect your lifecycle as an Android App Developer as well • Most of the Android Developer doesn’t know about Lifecycle clearly and

that lead to app’s quality problem •  Crash •  Malfunc$onal

Ac#vity Types

Ac$vity

ListAc$vity PreferenceAc$vity LauncherAc$vity FragmentAc$vity

Ac$onBarAc$vity

AppCompatAc$vity