android programming introduction

23
Android Programming Reading “The Big Nerd Ranch Guide” Bruce Tsai

Upload: yi-lung-tsai

Post on 19-May-2015

304 views

Category:

Software


1 download

DESCRIPTION

An introduction to Android programming with fundamental concepts, including activity lifecycle and fragment. The slides are based on book "The Big Nerd Ranch Guide" and some images are from site of Android Developer.

TRANSCRIPT

Page 1: Android programming introduction

Android ProgrammingReading “The Big Nerd Ranch Guide” Bruce Tsai

Page 2: Android programming introduction

Activity States

Resumed (Running)

Paused

Stopped

2

Page 3: Android programming introduction

Text

Activity LifecycleBack? Home?

3

Page 4: Android programming introduction

Text

Back StackIntact Last in, first out

4

Page 5: Android programming introduction

Multitasks

Task is cohesive unit that can move to “background” when user begins a new task or go to Home screen

5

Page 6: Android programming introduction

Text

Start Other ActivityVia ActivityManager

6

Page 7: Android programming introduction

Intent

Object that component uses to communicate with OS

Explicit intent — start activities within application

create Intent with Context and Class

Implicit intent — start activities in another application

7

Page 8: Android programming introduction

Intent i = new Intent(QuizActivity.this, CheatActivity.class);!

startActivity(i);

8

Page 9: Android programming introduction

Put Intent Extras (parent)

Intent i = new Intent(QuizActivity.this, CheatActivity.class);!

i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, true);!

startActivity(i);

9

Page 10: Android programming introduction

Use Intent Extra (child)mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

10

Page 11: Android programming introduction

11

Page 12: Android programming introduction

Get Activity Result

Intent i = new Intent(QuizActivity.this, CheatActivity.class);!

i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, true);!

startActivity(i);!

startActivityForResult(i, 0);

12

Page 13: Android programming introduction

Set Result (child)Intent data = new Intent();!

data.putExtra(EXTRA_ANSWER_SHOWN, true);!

setResult(RESULT_OK, data);

13

Page 14: Android programming introduction

Handle Result (parent)protected void onActivityResult(int requestCode, int resultCode, Intent data) {!

! if (data == null) return;!

! mIsCheater = !data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);!

}

14

Page 15: Android programming introduction

FragmentController object that activity can deputize to perform tasks

15

Page 16: Android programming introduction

Support LibraryFragment is introduced from API level 11

16

Page 17: Android programming introduction

Fragment HostActivity defines a spot in its layout for fragment’s view

Activity manage lifecycle of fragment instance

Hosting approaches: add to layout or add to code

17

Page 18: Android programming introduction

Fragment Manager

18

Page 19: Android programming introduction

Fragment Transaction

Usages of fragment in activities are add, remove, replace and perform other actions

Each set of changes that is committed to activity is fragment transaction

19

Page 20: Android programming introduction

FragmentManager fm = getSupportFragmentManager();!

Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);!

if (fragment == null) {!

! fragment = new CrimeFragment();!

! fm.beginTransaction()! .add(R.id.fragmentContainer)! .commit();!

}

20

Page 21: Android programming introduction

SDK Versions

Minimum SDK version — hard floor below which the OS should refuse to install the app

Target SDK version — API level the app is designed to run on

Build SDK version — private information between you and compiler

21

Page 22: Android programming introduction

dp (dip) — density-independent pixel

sp — scale-independent pixel

22

Page 23: Android programming introduction

<Button ! android:layout_width=“wrap_content”! android:layout_weight=“1” />!

<CheckBox ! android:layout_width=“wrap_content”! android:layout_weight=“1” />