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

Android University - Week 6

Caren
December 13, 2018
190

Android University - Week 6

Caren

December 13, 2018
Tweet

Transcript

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

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

    posts • User can pull to refresh the most recent 20 posts
  3. 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)
  4. Bottom Navigation Bar • Good for:
 - Top-level destinations that

    need to be accessible from anywhere in the app

  5. 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
  6. What are Fragments? • Pretty similar to activities! (but cannot

    live by itself, must be embedded in an Activity)
  7. 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
  8. 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) { } }
  9. 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
  10. 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) { } });
  11. 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) { } });
  12. Activities and Fragments // Replace the contents of the container

    with the new fragment getSupportFragmentManager() .beginTransaction() .replace(R.id.flContainer, new FooFragment()) .commit(); MainActivity.java
  13. 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
  14. In Summary Fragments are like ‘mini’ Activities
 made up of

    Java class + XML file
 has their own lifecycle callbacks

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

    Java class + XML file
 has their own lifecycle callbacks
 must live in an Activity
  16. 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…)