android programming introduction
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
Android ProgrammingReading “The Big Nerd Ranch Guide” Bruce Tsai
Activity States
Resumed (Running)
Paused
Stopped
2
Text
Activity LifecycleBack? Home?
3
Text
Back StackIntact Last in, first out
4
Multitasks
Task is cohesive unit that can move to “background” when user begins a new task or go to Home screen
5
Text
Start Other ActivityVia ActivityManager
6
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
Intent i = new Intent(QuizActivity.this, CheatActivity.class);!
startActivity(i);
8
Put Intent Extras (parent)
Intent i = new Intent(QuizActivity.this, CheatActivity.class);!
i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, true);!
startActivity(i);
9
Use Intent Extra (child)mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
10
11
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
Set Result (child)Intent data = new Intent();!
data.putExtra(EXTRA_ANSWER_SHOWN, true);!
setResult(RESULT_OK, data);
13
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
FragmentController object that activity can deputize to perform tasks
15
Support LibraryFragment is introduced from API level 11
16
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
Fragment Manager
18
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
FragmentManager fm = getSupportFragmentManager();!
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);!
if (fragment == null) {!
! fragment = new CrimeFragment();!
! fm.beginTransaction()! .add(R.id.fragmentContainer)! .commit();!
}
20
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
dp (dip) — density-independent pixel
sp — scale-independent pixel
22
<Button ! android:layout_width=“wrap_content”! android:layout_weight=“1” />!
<CheckBox ! android:layout_width=“wrap_content”! android:layout_weight=“1” />