Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Pirate's Guidelines for Android

A Pirate's Guidelines for Android

Fragments suck. State is hard. Android is hostile. How can you write a crashfree, bug free app on such a buggy, fragmented OS? In the spirit of Jack Sparrow, I’ve come up with some guidelines for making Android development less of a headache. Talk would basically be about how we’ve adapted and used each of these in the Electric Objects companion app.

- There is a one to one correspondence between a wireframe screen and an Activity.
- Every Activity has it's own layout file.
- Reusable View code belongs in a View class.
- Fragments are not simple. Don't use fragments.
- Embrace state machines.
- Corollary: Every Activity is a State Machine.
- Override on configuration changed at your own peril (aka never)
- Retain nothing but state (and hardware adapters)

This talk was first given at DroidCon NYC 2015.
http://droidcon.nyc/2015/dcnyc/10/

67867d2677e7dd983000441ff0b1c089?s=128

NeiL saitug

August 27, 2015
Tweet

More Decks by NeiL saitug

Other Decks in Programming

Transcript

  1. Android Development The Pirate's CODE

  2. None
  3. None
  4. Android Development The Pirate's Guidelines

  5. Where did these come from?

  6. Where did these come from? How Do I Start An

    Android App?
  7. None
  8. None
  9. None
  10. How Do I Start An Android App?

  11. How do I map these mock ups to Android 'Pieces'?

  12. None
  13. None
  14. None
  15. None
  16. Pirate Guideline #1

  17. Pirate Guideline #1 There is a one to one correspondence

    between a wireframe screen and an Activity
  18. Super easy to get started writing a new application

  19. None
  20. None
  21. None
  22. OkayActivity.java setContentView(R.layout.activity_generic.xml); SuccessActivity.java setContentView(R.layout.activity_generic.xml);

  23. None
  24. None
  25. luke: hey lisa! !

  26. luke: hey lisa! ! lisa: hey! !!

  27. luke: the app's looking really great! !!

  28. luke: the app's looking really great! !! lisa: /me blushes

    ☺️
  29. luke: just one thing

  30. luke: just one thing luke: *explains how someone could get

    stuck*
  31. luke: we need a logout button

  32. None
  33. None
  34. lisa: /me prepares diatribe on the Android Back Button

  35. lisa: /me realizes she is vastly outnumbered by iOS devices

  36. None
  37. lisa: /me ctrl-C; ctrl-V

  38. OkayActivity.java setContentView(R.layout.activity_generic.xml); SuccessActivity.java setContentView(R.layout.activity_slightly_less_generic.xml);

  39. Pirate Guideline #2

  40. Pirate Guideline #2 Every Activity has its own layout file

  41. None
  42. None
  43. PRGRAMMING BSET PRACTICES PRINCIPAL 101

  44. PRGRAMMING BSET PRACTICES PRINCIPAL 101 DRY: Don't Repeat Yourself

  45. None
  46. Pirate Guideline #3

  47. Pirate Guideline #3 Reusable View code belongs in a View

    class
  48. None
  49. None
  50. PRGRAMMING BSET PRACTICES PRINCIPAL 101

  51. PRGRAMMING BSET PRACTICES PRINCIPAL 101 Favor composition over inheritance

  52. None
  53. None
  54. None
  55. None
  56. Pirate Guideline #3A Reusable View attributes belong in a style

  57. None
  58. None
  59. None
  60. None
  61. None
  62. None
  63. None
  64. None
  65. None
  66. None
  67. None
  68. !""""

  69. Pirate Guideline #4

  70. Pirate Guideline #4 Fragments are not simple. Don't use fragments.*

  71. Pirate Guideline #4 Fragments are not simple. Don't use fragments.*

    *unless it is absolutely necessary
  72. None
  73. @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); }

  74. @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); \\ ~~something real

    hacky~~ }
  75. —languages not changing correctly

  76. —languages not changing correctly —views not resizing properly

  77. —languages not changing correctly —views not resizing properly —strange invocations

    to .onSaveInstanceState() in onConfigurationChanged
  78. —BUGGY —HARD TO TEST —HARD TO FIX

  79. Pirate Guideline #5

  80. Pirate Guideline #5 Never override configuration changed.*

  81. Pirate Guideline #5 Never override configuration changed.* * for view

    code
  82. setRetainState(true);

  83. setRetainState(true); How well do you really know the Activity Lifecycle?

  84. None
  85. @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // save my

    state! }
  86. Pirate Guideline #6 Retain nothing but state*

  87. Pirate Guideline #6 Retain nothing but state* *and hardware adapters

  88. CAVEAT:

  89. CAVEAT: headless fragments

  90. None
  91. None
  92. None
  93. Pirate Guideline #7

  94. Pirate Guideline #7 Embrace state machines

  95. A State Machine —States are explicitly defined —Can only do

    certain things in each state —Where you can go next depends on where you are now
  96. None
  97. None
  98. None
  99. None
  100. Android Development A Pirate's Guidelines

  101. —There is a one to one correspondence between a wireframe

    screen and an Activity —Every Activity has its own layout file —Reusable View code belongs in a View class —Fragments are not simple. Don't use fragments.* —Never override configuration changed* —Retain nothing but state* —Embrace state machines
  102. ~thank you~

  103. Lisa Neigut work @electricobjects me on the internet, @niftynei