Lecture #3
Activities && Intents
General Stuff
- Facebook Group
- Udacity Lesson #3
- Study Jam Group Signup
- My ActionBar looks different
- 23/2 No lesson
Udacity Lesson Overview
- Listeners
- Toast
- UP Navigation vs Back Navigation(TASK)
- StartActivity
- Intent (bundle) Implicitly & Explicitly
- Fragment
- Settings
- SharedPreferences
- BroadCast Receivers
Toast
Context
- Global information about an application
environment
- Loading resources, launching a new
Activity, obtaining a system service,
getting internal file paths, and creating
views
- Biggest memory leak potential (latter in
our sessions)
Listeners
Interface related to view with
single callback.
Will be called when event is
occur
Event Listeners
onClick() - View.OnClickListener
onLongClick() - View.OnLongClickListener
onFocusChange() -
View.OnFocusChangeListener
onKey() - View.OnKeyListener
onTouch() - View.OnTouchListener
onCreateContextMenu() - View guess what :)
But?
- Hard to maintain
- Hard to find which button do what
- Every class in Java, including anonymous
inner classes which create an object and
writes accessor methods internally, uses
about 500 bytes of code.
Navigation UP and Back
- Task and Backstack
- Back navigation is within Task
- UP is hierarchical navigation
Intent
- Has address
- Has data map: key = value
- Has schema
You can pass any primitive types and also
serializable/Parcelable objects
Used to send data/action across the system
StartActivity
Explicitly:
Implicitly:
But what if?
But what if?
But what if?
- Non readable
- Mix of static variables
- Other activities should know what target
expect
Solution
Solution
Fragment
The biggest advantage
and pain
- .commit() asynchronous
- any operation after state of Activity was
saved
- Getting “Context” within Fragment
Solution
- Use callbacks when possible.
- Manage internal state of activity and
fragment
- DialogFragment special treatment
SharedPreferences
- Great thing to store persistent various.
- Not accessible by other apps
- Used to save things between session
and session
- Sometimes could be great
communication tool (like intent) using
“onSharedPreferenceChangeListener”
But...
- Don’t forget that each commit() is IO
operation.
- use Apply() instead
Broadcast Receivers
Great guy :)
- Loose Coupling
- 1-to-n relationship
- The onReceive() method is always executed on the
main thread
- You can notify components in your entire application, so
the communicating components do not have to "see"
each other.
But...
- Marshaling data via intent really hard
- Register/Unregister it when you do not needed him
(BaseActivity)
- Not impossible to transfer large objects
Lecture #3: Android Academy Study Jam
Lecture #3: Android Academy Study Jam
Lecture #3: Android Academy Study Jam

Lecture #3: Android Academy Study Jam

  • 1.
  • 2.
    General Stuff - FacebookGroup - Udacity Lesson #3 - Study Jam Group Signup - My ActionBar looks different - 23/2 No lesson
  • 3.
    Udacity Lesson Overview -Listeners - Toast - UP Navigation vs Back Navigation(TASK) - StartActivity - Intent (bundle) Implicitly & Explicitly - Fragment - Settings - SharedPreferences - BroadCast Receivers
  • 4.
  • 5.
    Context - Global informationabout an application environment - Loading resources, launching a new Activity, obtaining a system service, getting internal file paths, and creating views - Biggest memory leak potential (latter in our sessions)
  • 6.
    Listeners Interface related toview with single callback. Will be called when event is occur
  • 7.
    Event Listeners onClick() -View.OnClickListener onLongClick() - View.OnLongClickListener onFocusChange() - View.OnFocusChangeListener onKey() - View.OnKeyListener onTouch() - View.OnTouchListener onCreateContextMenu() - View guess what :)
  • 9.
    But? - Hard tomaintain - Hard to find which button do what - Every class in Java, including anonymous inner classes which create an object and writes accessor methods internally, uses about 500 bytes of code.
  • 11.
    Navigation UP andBack - Task and Backstack - Back navigation is within Task - UP is hierarchical navigation
  • 12.
    Intent - Has address -Has data map: key = value - Has schema You can pass any primitive types and also serializable/Parcelable objects Used to send data/action across the system
  • 13.
  • 14.
  • 15.
  • 16.
    But what if? -Non readable - Mix of static variables - Other activities should know what target expect
  • 17.
  • 18.
  • 19.
    Fragment The biggest advantage andpain - .commit() asynchronous - any operation after state of Activity was saved - Getting “Context” within Fragment
  • 20.
    Solution - Use callbackswhen possible. - Manage internal state of activity and fragment - DialogFragment special treatment
  • 21.
    SharedPreferences - Great thingto store persistent various. - Not accessible by other apps - Used to save things between session and session - Sometimes could be great communication tool (like intent) using “onSharedPreferenceChangeListener”
  • 23.
    But... - Don’t forgetthat each commit() is IO operation. - use Apply() instead
  • 24.
    Broadcast Receivers Great guy:) - Loose Coupling - 1-to-n relationship - The onReceive() method is always executed on the main thread - You can notify components in your entire application, so the communicating components do not have to "see" each other.
  • 25.
    But... - Marshaling datavia intent really hard - Register/Unregister it when you do not needed him (BaseActivity) - Not impossible to transfer large objects