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

Android University - Week 6

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=47 Caren
December 13, 2018
110

Android University - Week 6

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=128

Caren

December 13, 2018
Tweet

Transcript

  1. Android University Week 6: Instagram (Part 2)

  2. Instagram (Part 1) - User can create a new account

    - User can log in / out - User can create a post with a photo and caption
  3. Instagram (Part 2) • User can view most recent 20

    posts
  4. Instagram (Part 2) • User can view most recent 20

    posts • User can pull to refresh the most recent 20 posts
  5. Instagram (Part 2) • User can view most recent 20

    posts • User can pull to refresh the most recent 20 posts • The user can switch between different tabs
 - viewing all posts (feed)
 - capture (creation)
 - profile tabs (stream)
  6. None
  7. Bottom Navigation Bar

  8. Bottom Navigation Bar • Good for:
 - Top-level destinations that

    need to be accessible from anywhere in the app

  9. Bottom Navigation Bar • Good for:
 - Top-level destinations that

    need to be accessible from anywhere in the app
 • NOT good for:
 - Obscure tasks the user never really needs
  10. Fragments

  11. What are Fragments? • Pretty similar to activities! (but cannot

    live by itself, must be embedded in an Activity)
  12. What are Fragments? • Pretty similar to activities! (but cannot

    live by itself, must be embedded in an Activity) • Can have multiple Fragments on the screen at the same time
  13. Fragments and Nav Bars

  14. Fragments and Nav Bars CREATE

  15. Fragments and Nav Bars HOME CREATE PROFILE

  16. How do we build Fragments?

  17. How do we build Fragments? • Similar to an Activity


    Java File + XML layout
  18. How do we build Fragments? • Similar to an Activity


    Java File + XML layout public class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { return inflater.inflate(R.layout.feed_fragment, parent); } // Any view setup should occur here. E.g., view lookups and attaching view listeners. @Override public void onViewCreated(View view, Bundle savedInstanceState) { } }
  19. How do we build Fragments? • Similar to an Activity


    Java File + XML layout public class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { return inflater.inflate(R.layout.feed_fragment, parent); } // Any view setup should occur here. E.g., view lookups and attaching view listeners. @Override public void onViewCreated(View view, Bundle savedInstanceState) { } } <RelativeLayout xmlns:android="http:// schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="@string/login_label" /> </RelativeLayout> R.layout.feed_fragment
  20. Fragment Lifecycle

  21. Fragment Lifecycle (the important parts) onCreateView() onViewCreated()

  22. Fragment Lifecycle (the important parts) onCreateView() - inflate fragment layout


    inflator.inflate(R.layout.feed_fragment, parent);
  23. Fragment Lifecycle (the important parts) onCreateView() - inflate fragment layout


    inflator.inflate(R.layout.feed_fragment, parent); onViewCreated() - setting up views and attaching listeners
 feedRecyclerView = findViewById(R.id.feedRv); button = findViewById(R.id.barrier); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });
  24. Fragment Lifecycle (the important parts) onCreateView() - inflate fragment layout


    inflator.inflate(R.layout.feed_fragment, parent); onViewCreated() - setting up views and attaching listeners
 feedRecyclerView = findViewById(R.id.feedRv); button = findViewById(R.id.barrier); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });
  25. Activities and Fragments

  26. Activities and Fragments // Replace the contents of the container

    with the new fragment getSupportFragmentManager() .beginTransaction() .replace(R.id.flContainer, new FooFragment()) .commit(); MainActivity.java
  27. Activities and Fragments // Replace the contents of the container

    with the new fragment getSupportFragmentManager() .beginTransaction() .replace(R.id.flContainer, new FooFragment()) .commit(); <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <FrameLayout android:id="@+id/flContainer" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> activity_main.xml MainActivity.java
  28. In Summary Fragments are like ‘mini’ Activities


  29. In Summary Fragments are like ‘mini’ Activities
 made up of

    Java class + XML file

  30. In Summary Fragments are like ‘mini’ Activities
 made up of

    Java class + XML file
 has their own lifecycle callbacks

  31. In Summary Fragments are like ‘mini’ Activities
 made up of

    Java class + XML file
 has their own lifecycle callbacks
 must live in an Activity
  32. In Summary Fragments are like ‘mini’ Activities
 made up of

    Java class + XML file
 has their own lifecycle callbacks
 must live in an Activity When Activities contain Fragments, the Activity controls which Fragment to show. The rest of the logic goes in the Fragment (setting up views, handling clicks, making network calls…)