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

Android Design Patterns

felipecsl
February 21, 2014

Android Design Patterns

Apresentação realizada na meetup do GDG Porto Alegre em 20/02/2014.

felipecsl

February 21, 2014
Tweet

More Decks by felipecsl

Other Decks in Programming

Transcript

  1. Brigita Fernežir on Feb 9, 2014 at 4:56 PM It

    fucking crashes all the time fix it !!! Usuários são cruéis
  2. DRY - DON’T REPEAT YOURSELF • Android Action Bar Style

    Generator 1. Best practices http://jgilfelt.github.io/android-actionbarstylegenerator
  3. 2. Open Source Por que? • Maior qualidade • Liberdade

    para fazer alterações • Aprendizado
  4. 2. Open Source Por que? • Maior qualidade • Liberdade

    para fazer alterações • Aprendizado • Visibilidade
  5. 2. Open Source UI e Compatibilidade Android Support Library v4

    • DrawerLayout, ActionBarDrawerToggle • Fragment • ViewPager • NotificationCompat http://developer.android.com/tools/support-library/features.html#v4
  6. 2. Open Source UI e Compatibilidade Android AppCompat v7 •

    ActionBar • ActionBarActivity • ShareActionProvider • GridLayout http://developer.android.com/tools/support-library/features.html#v7-appcompat
  7. 2. Open Source UI e Compatibilidade NineOldAndroids Backport da API

    de animações https://github.com/JakeWharton/NineOldAndroids/ AnimatorSet set = new AnimatorSet(); set.playTogether( ObjectAnimator.ofFloat(myView, "rotationX", 0, 360), ObjectAnimator.ofFloat(myView, "rotationY", 0, 180), ObjectAnimator.ofFloat(myView, "rotation", 0, -90), ObjectAnimator.ofFloat(myView, "translationX", 0, 90), ObjectAnimator.ofFloat(myView, "translationY", 0, 90), ObjectAnimator.ofFloat(myView, "scaleX", 1, 1.5f), ObjectAnimator.ofFloat(myView, "scaleY", 1, 0.5f), ObjectAnimator.ofFloat(myView, "alpha", 1, 0.25f, 1) ); set.setDuration(5 * 1000).start();
  8. 2. Open Source Caching e Networking DiskLruCache Disk-based LRU cache

    https://github.com/jakewharton/disklrucache
  9. 2. Open Source Caching e Networking Android-Async-Http Asynchronous HTTP https://github.com/loopj/android-async-http

    import org.json.*; import com.loopj.android.http.*; ! class TwitterRestClientUsage { public void getPublicTimeline() throws JSONException { TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() { @Override public void onSuccess(JSONArray timeline) { // Pull out the first event on the public timeline JSONObject firstEvent = timeline.get(0); String tweetText = firstEvent.getString("text"); ! // Do something with the response System.out.println(tweetText); } }); } }
  10. 2. Open Source Caching e Networking Http-Request https://github.com/kevinsawicki/http-request String response

    = HttpRequest.get("http://google.com").body(); System.out.println("Response was: " + response);
  11. 2. Open Source Comunicação e Concorrência Otto Event Bus http://square.github.io/otto/

    Bus bus = new Bus(); bus.post(new AnswerAvailableEvent(42)); ! //... ! @Subscribe public void answerAvailable(AnswerAvailableEvent event) { // TODO: React to the event somehow! }
  12. 2. Open Source Dependency Injection Dagger http://square.github.io/dagger/ class CoffeeMaker {

    @Inject Heater heater; @Inject Pump pump; ! ... } ! class Thermosiphon implements Pump { private final Heater heater; ! @Inject Thermosiphon(Heater heater) { this.heater = heater; } ! ... }
  13. 2. Open Source Dependency Injection Butter Knife https://github.com/JakeWharton/butterknife View Injection

    class ExampleActivity extends Activity { @InjectView(R.id.user) EditText username; @InjectView(R.id.pass) EditText password; ! @OnClick(R.id.submit) void submit() { // TODO call server... } ! @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); // TODO Use "injected" views... } }
  14. 3. Code design patterns • Auto-contida • Libera recursos ao

    ser finalizada • Não é dependência de objetos globais Activity • Delega responsabilidades
  15. 3. Code design patterns Activity Auto-contida Inputs e outputs definidos

    via Intent/Bundle Implementa onSaveInstanceState Não possui lógica de negócio
  16. 3. Code design patterns Activity Libera recursos ao ser finalizada

    @Override public void onDestroy() { super.onDestroy(); BusProvider.getInstance().unregister(this); }
  17. 3. Code design patterns Activity Não é dependência de objetos

    globais @Override public void onCreate(Bundle savedState) { super.onCreate(savedState); LongLivedObject obj = LongLivedObject.getInstance(); obj.doSomething(this); // bad } Exemplo do que não fazer:
  18. 3. Code design patterns Performance Utilize a UI Thread apenas

    para layout! Mova tarefas paralelas para o background
  19. 3. Code design patterns Performance Utilize a UI Thread apenas

    para layout! Mova tarefas paralelas para o background Pense um pouco antes de instanciar objetos
  20. 3. Code design patterns Performance Utilize a UI Thread apenas

    para layout! Mova tarefas paralelas para o background Pense um pouco antes de instanciar objetos Cancele AsyncTasks ao finalizar a Activity