android mypractices

26
Android Best Practices Murat Yener, eteration Wednesday, May 23, 12

Upload: murat-yener

Post on 17-May-2015

640 views

Category:

Technology


1 download

DESCRIPTION

Talk from Android Developer Days and Istanbul GTUG (GDG) meetings.

TRANSCRIPT

Page 1: Android mypractices

Android Best PracticesMurat Yener, eteration

Wednesday, May 23, 12

Page 2: Android mypractices

Android Best PracticesMurat Yener, eteration

Wednesday, May 23, 12

Page 3: Android mypractices

Android Best PracticesMurat Yener, eteration

Wednesday, May 23, 12

Page 4: Android mypractices

Android Best PracticesMurat Yener, eteration

Wednesday, May 23, 12

Page 5: Android mypractices

Why am I doing this talk?

Get well soon Alain!!

Wednesday, May 23, 12

Page 6: Android mypractices

Alternatives

• Make me look like Alain?

• or just let me do another talk...

Wednesday, May 23, 12

Page 7: Android mypractices

Alternatives

• Make me look like Alain?

• or just let me do another talk...

Wednesday, May 23, 12

Page 8: Android mypractices

pointed ears

science officer

half human - half vulcan

trying to understand human nature

rounded ears

developer

half flex - half java

trying to understand objective-c behaviour

Wednesday, May 23, 12

Page 9: Android mypractices

how to develop?

• Android SDK: Java, capable, resources, easy

• Android NDK: Most capable but??

• Adobe AIR: easy XML+AS but??!!

• runtime, memory, new apis

• Web+JS

Wednesday, May 23, 12

Page 10: Android mypractices

SDK

• Activities

• Services

• Widgets

• Intents&Broadcasts

• Preferences

Wednesday, May 23, 12

Page 11: Android mypractices

Going to Market

• Silence my Phone vs Shake to Answer

• idea

• target devices

• code right

• let users communicate with you

• respond back... (even when you don’t care)

Wednesday, May 23, 12

Page 12: Android mypractices

Targeting all devices

• Matching everyone is winner!

• bug!! v5 thinks its v6

• canary pattern

Wednesday, May 23, 12

Page 13: Android mypractices

Targeting all devices

• Matching everyone is winner!

• bug!! v5 thinks its v6

• canary pattern

Wednesday, May 23, 12

Page 14: Android mypractices

Targeting all Devices

• Never, ever manipulate images!!!

• Ok, only if you must...

• nine-patches

Wednesday, May 23, 12

Page 15: Android mypractices

Targeting all Devices

• Never, ever manipulate images!!!

• Ok, only if you must...

• nine-patches

Wednesday, May 23, 12

Page 16: Android mypractices

Targeting all Devices

• Never, ever manipulate images!!!

• Ok, only if you must...

• nine-patches

Wednesday, May 23, 12

Page 17: Android mypractices

Targeting all Devices

• Never, ever manipulate images!!!

• Ok, only if you must...

• nine-patches

Wednesday, May 23, 12

Page 18: Android mypractices

app fitness

• Collect you own trash!

• Services, unregister whatever registered

• Images: image=null != image.recycle();

• favor primitives over objects

• BUT!! avoid autoboxing

• keep in mind Generics only work for objects

• size checks on loops

Wednesday, May 23, 12

Page 19: Android mypractices

things that take time

• Specially when network is involved

• Never, ever lock up UI thread!!!

• Painless threading: Use AsyncTasks

• Let user know: Toaster

Wednesday, May 23, 12

Page 20: Android mypractices

Responsive UI

• use as little as possibles views in views: slower layout, drawing, startup time...

• cut down view hierarchy: memory, slow, StackOverFlowException

Wednesday, May 23, 12

Page 21: Android mypractices

Less Views??

• use Merge as root tag for components

• use RelativeLayout instead of LinearLayout

Wednesday, May 23, 12

Page 22: Android mypractices

class AndroidWay extends Activity {     TextView name;     ImageView thumbnail;     LocationManager loc;     Drawable icon;     String myName;

    public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);        name      = (TextView) findViewById(R.id.name);         thumbnail = (ImageView) findViewById(R.id.thumbnail);         loc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);         icon      = getResources().getDrawable(R.drawable.icon);         myName    = getString(R.string.app_name);         name.setText( "Hello, " + myName );     } } class RoboWay extends RoboActivity {     @InjectView(R.id.name)             TextView name;     @InjectView(R.id.thumbnail)        ImageView thumbnail;     @InjectResource(R.drawable.icon)   Drawable icon;     @InjectResource(R.string.app_name) String myName;     @Inject                            LocationManager loc;

    public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);        name.setText( "Hello, " + myName );     } }

DI in Android!!

• findViewById is expensive!

• either use it once or... RoboGuice!!

Wednesday, May 23, 12

Page 23: Android mypractices

...

Wednesday, May 23, 12

Page 24: Android mypractices

@EActivity(R.layout.translate) // Sets content view to R.layout.translatepublic class TranslateActivity extends Activity { @ViewById // Injects R.id.textInput EditText textInput;

@ViewById(R.id.myTextView) // Injects R.id.myTextView TextView result;

@AnimationRes // Injects android.R.anim.fade_in Animation fadeIn;

@Click // When R.id.doTranslate button is clicked void doTranslate() { translateInBackground(textInput.getText().toString()); } @Background // Executed in a background thread void translateInBackground(String textToTranslate) { String translatedText = callGoogleTranslate(textToTranslate); showResult(translatedText); } @UiThread // Executed in the ui thread void showResult(String translatedText) { result.setText(translatedText); result.startAnimation(fadeIn); }

AndroidAnnotations

Wednesday, May 23, 12

Page 25: Android mypractices

Wednesday, May 23, 12

Page 26: Android mypractices

</presentation>

[email protected]/yenermdevchronicles.com

blogs.eteration.com/blogs

Wednesday, May 23, 12