was it frag ment to be
play

Was it Frag-Ment to be? Luke Sleeman - Freelance Android developer - PowerPoint PPT Presentation

Was it Frag-Ment to be? Luke Sleeman - Freelance Android developer http://lukesleeman.com luke.sleeman@gmail.com @LukeSleeman Was it Frag-Ment to be? Intro - What are fragments? History Problems with fragments - Lifecycle, Bugs, Hard


  1. Was it Frag-Ment to be? Luke Sleeman - Freelance Android developer http://lukesleeman.com luke.sleeman@gmail.com @LukeSleeman

  2. Was it Frag-Ment to be? • Intro - What are fragments? History • Problems with fragments - Lifecycle, Bugs, Hard to use right • Best Practises: Don't use them - Places where we might have used fragments, alternatives • Best Practises: Risk mitigation - Googles suggestions • Summary - What we learnt, key takeaways • Questions

  3. Intro

  4. What is a fragment? A UI control you can compose to build responsive tablet UI’s

  5. What is a fragment? A UI control you can compose to build responsive tablet UI’s

  6. What is a fragment? A reusable group of Views to build our ui.

  7. What is a fragment? A reusable group of Views to build our ui.

  8. What is a fragment? An application component with a lifecycle

  9. What is a fragment? An application component with a lifecycle

  10. History - Android 2.x

  11. History - Android 3.x

  12. History - Android 4.x

  13. Problems

  14. Problem - Fragments ‘sorta’ work

  15. ‘Sorta’ works is the worst kind of works

  16. Problem - Lifecycle

  17. Problem - Lifecycle

  18. • Add in asynchronous fragment transactions • Child fragments • Viewpagers which cache state and lazy construct fragments … • Ultimately you have no idea what state your fragment will be in at any point in time.

  19. Problem - Communication List Fragment ? ??? Activity ? Detail Fragment

  20. 
 
 
 
 Problems - Lifecycle & Communication private void updateTimeStamps(){ 
 ... 
 String format = getContext().getString(R.string…) ... 
 } 
 private void newDataReceived(List<> ...){ 
 ... 
 updateTimeStamps(); 
 ... 
 } 
 private void setTimeZone(TimeZone timeZone){ 
 ... 
 updateTimeStamps(); 
 ... 
 } 
 public void startDataRefresh(){ 
 ... 
 setTimeZone(TimeZone. getDefault ()); 
 ... 
 }

  21. 
 
 
 
 Problems - Lifecycle & Communication private void updateTimeStamps(){ 
 ... 
 String format = getContext().getString(R.string…) ... 
 } 
 private void newDataReceived(List<> ...){ 
 ... 
 updateTimeStamps(); 
 ... 
 } 
 private void setTimeZone(TimeZone timeZone){ 
 ... 
 updateTimeStamps(); 
 ... 
 } 
 public void startDataRefresh(){ 
 ... 
 setTimeZone(TimeZone. getDefault ()); 
 ... 
 }

  22. 
 
 
 
 Problems - Lifecycle & Communication private void updateTimeStamps(){ 
 ... 
 String format = getContext().getString(R.string…) ... 
 } 
 private void newDataReceived(List<> ...){ 
 ... 
 updateTimeStamps(); 
 ... 
 } 
 private void setTimeZone(TimeZone timeZone){ 
 ... 
 updateTimeStamps(); 
 ... 
 } 
 public void startDataRefresh(){ 
 ... 
 setTimeZone(TimeZone. getDefault ()); 
 ... 
 }

  23. 
 
 
 
 Problems - Lifecycle & Communication private void updateTimeStamps(){ 
 ... 
 String format = getContext().getString(R.string…) ... 
 } 
 private void newDataReceived(List<> ...){ 
 ... 
 updateTimeStamps(); 
 ... 
 } 
 private void setTimeZone(TimeZone timeZone){ 
 ... 
 updateTimeStamps(); 
 ... 
 } 
 public void startDataRefresh(){ 
 ... 
 setTimeZone(TimeZone. getDefault ()); 
 ... 
 }

  24. Problems (from google) • “ Fragments are so general they become really difficult to explain to people ” • “ A lot of times, you build up a fragment transaction, commit it, and things work completely differently from how you expect ” • Even more lifecycle to manage made stuff to complicated - “ and the bugs didn’t help! ” • People think ‘they are just like views’. <fragment> tag turned out to be a bad bit of syntactic sugar.

  25. Summary - Problems 0. They almost work! But: 1. Lifecycle makes them prone to programmer error. 2. No clear use case - Replace views? Replace activities? Tablet UIs? 3. No guarantees about what state your fragment will be in.

  26. Best Practises 1 - Don’t use them! 2 - Mitigate the risks if you do use them

  27. Don’t use them - Normal Screens

  28. Don’t use them - Tablet UI’s

  29. Don’t use them - composable parts of UI Use a ViewGroup! (Subclass LinearLayout or FrameLayout)

  30. 
 
 
 
 Subclass ViewGroup public class UserSummaryView extends LinearLayout { 
 private ImageView profileImage; 
 private TextView name; 
 private TextView title; 
 private TextView bio; 
 public UserSummaryView(Context context) { 
 super(context); 
 init(context); 
 } 
 public UserSummaryView(Context context, AttributeSet attrs) { 
 super(context, attrs); 
 init(context); 
 } 
 public UserSummaryView(Context context, AttributeSet attrs, int defStyleAttr) { 
 super(context, attrs, defStyleAttr); 
 init(context); 
 }

  31. 
 
 
 
 Subclass ViewGroup public class UserSummaryView extends LinearLayout { 
 private ImageView profileImage; 
 private TextView name; 
 private TextView title; 
 private TextView bio; 
 public UserSummaryView(Context context) { 
 super(context); 
 init(context); 
 } 
 public UserSummaryView(Context context, AttributeSet attrs) { 
 super(context, attrs); 
 init(context); 
 } 
 public UserSummaryView(Context context, AttributeSet attrs, int defStyleAttr) { 
 super(context, attrs, defStyleAttr); 
 init(context); 
 }

  32. 
 
 
 
 Subclass ViewGroup public class UserSummaryView extends LinearLayout { 
 private ImageView profileImage; 
 private TextView name; 
 private TextView title; 
 private TextView bio; 
 public UserSummaryView(Context context) { 
 super(context); 
 init(context); 
 } 
 public UserSummaryView(Context context, AttributeSet attrs) { 
 super(context, attrs); 
 init(context); 
 } 
 public UserSummaryView(Context context, AttributeSet attrs, int defStyleAttr) { 
 super(context, attrs, defStyleAttr); 
 init(context); 
 }

  33. 
 
 Subclass ViewGroup private void init(Context context){ setOrientation( VERTICAL ); LayoutInflater. from (context).inflate( R.layout. view_user_summary , this); 
 profileImage = (ImageView)findViewById(R.id. user_summary_image ); 
 name = (TextView)findViewById(R.id. user_summary_name ); 
 title = (TextView)findViewById(R.id. user_summary_title ); 
 bio = (TextView)findViewById(R.id. user_summary_bio ); 
 } 
 public void setUser(User user){ 
 Picasso. with (getContext()) .load(user.getProfileImageUrl()) .into(profileImage); 
 name.setText(user.getName()); 
 title.setText(user.getJobTitle()); 
 bio.setText(user.getBio()); 
 }

  34. 
 
 Subclass ViewGroup private void init(Context context){ setOrientation( VERTICAL ); LayoutInflater. from (context).inflate( R.layout. view_user_summary , this); 
 profileImage = (ImageView)findViewById(R.id. user_summary_image ); 
 name = (TextView)findViewById(R.id. user_summary_name ); 
 title = (TextView)findViewById(R.id. user_summary_title ); 
 bio = (TextView)findViewById(R.id. user_summary_bio ); 
 } 
 public void setUser(User user){ 
 Picasso. with (getContext()) .load(user.getProfileImageUrl()) .into(profileImage); 
 name.setText(user.getName()); 
 title.setText(user.getJobTitle()); 
 bio.setText(user.getBio()); 
 }

  35. 
 
 Subclass ViewGroup <?xml version="1.0" encoding="utf-8"?> 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:orientation="vertical" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 <ImageView 
 android:id="@+id/user_summary_image" 
 android:layout_width="match_parent" 
 android:layout_height="256dp" 
 tools:src="@drawable/profile_sample" 
 android:scaleType="centerCrop" 
 /> 
 <TextView 
 tools:text="Luke Sleeman" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/user_summary_name" …

  36. 
 
 Subclass ViewGroup <?xml version="1.0" encoding="utf-8"?> 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:orientation="vertical" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 <ImageView 
 android:id="@+id/user_summary_image" 
 android:layout_width="match_parent" 
 android:layout_height="256dp" 
 tools:src="@drawable/profile_sample" 
 android:scaleType="centerCrop" 
 /> 
 <TextView 
 tools:text="Luke Sleeman" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/user_summary_name" …

Recommend


More recommend