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

Android University - Week 2

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=47 Caren
December 07, 2018
280

Android University - Week 2

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=128

Caren

December 07, 2018
Tweet

Transcript

  1. Android University Week 2: Flicks Android App, Continued

  2. Flicks App Portrait Landscape

  3. Flicks App So far, our Flicks app can: • Display

    a list of currently playing movies • Each movie item has info including title, image, and overview

  4. Flicks App So far, our Flicks app can: • Display

    a list of currently playing movies (Activity, RecyclerView) • Each movie item has info including title, image, and overview (Layouts, ImageViews, network calls)

  5. RecyclerView.Adapter public class EmailAdapter extends RecyclerView.Adapter<EmailAdapter.ViewHolder> { List<Email> emailsToDisplay; //

    Provide a direct reference to each of the views within a data item // Used to cache the views within the item layout for fast access public class ViewHolder extends RecyclerView.ViewHolder { } // Usually involves inflating a layout from XML and returning the holder @Override public ContactsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { } // Involves populating data into the item through holder @Override public void onBindViewHolder(ContactsAdapter.ViewHolder viewHolder, int position) { } }
  6. RecyclerView.Adapter public class EmailAdapter extends RecyclerView.Adapter<EmailAdapter.ViewHolder> { List<Email> emailsToDisplay; //

    Provide a direct reference to each of the views within a data item // Used to cache the views within the item layout for fast access public class ViewHolder extends RecyclerView.ViewHolder { public TextView nameTextView; public TextView messageTitleTextView; // We also create a constructor that accepts the entire item row // and does the view lookups to find each subview public ViewHolder(View itemView) { // Stores the itemView in a public final member variable that can be used // to access the context from any ViewHolder instance. super(itemView); nameTextView = (TextView) itemView.findViewById(R.id.name); messageTitleTextView = (TextView) itemView.findViewById(R.id.messageTitle); } } // Usually involves inflating a layout from XML and returning the holder @Override public ContactsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { } // Involves populating data into the item through holder @Override public void onBindViewHolder(ContactsAdapter.ViewHolder viewHolder, int position) { } }
  7. RecyclerView.Adapter public class EmailAdapter extends RecyclerView.Adapter<EmailAdapter.ViewHolder> { List<Email> emailsToDisplay; //

    Provide a direct reference to each of the views within a data item // Used to cache the views within the item layout for fast access public class ViewHolder extends RecyclerView.ViewHolder { } // Usually involves inflating a layout from XML and returning the holder @Override public ContactsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { Context context = parent.getContext(); LayoutInflater inflater = LayoutInflater.from(context); // Inflate the custom layout View emailItemView = inflater.inflate(R.layout.item_email, parent, false); // Return a new holder instance ViewHolder viewHolder = new ViewHolder(emailItemView); return viewHolder; } // Involves populating data into the item through holder @Override public void onBindViewHolder(ContactsAdapter.ViewHolder viewHolder, int position) { } }
  8. RecyclerView.Adapter public class EmailAdapter extends RecyclerView.Adapter<EmailAdapter.ViewHolder> { List<Email> emailsToDisplay; //

    Provide a direct reference to each of the views within a data item // Used to cache the views within the item layout for fast access public class ViewHolder extends RecyclerView.ViewHolder { } // Usually involves inflating a layout from XML and returning the holder @Override public ContactsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { } // Involves populating data into the item through holder @Override public void onBindViewHolder(ContactsAdapter.ViewHolder viewHolder, int position) { // Get the data model based on position Email email = emailsToDisplay.get(position); // Set item views based on your views and data model viewHolder.nameTextView.setText(email.getName()); viewHolder.messageTitleTextView.setText(email.getTitle()); } }
  9. None
  10. Flicks App This week, we will continue working on the

    Flicks app to add the following features: • Allow users to tap on a movie to see specific details about the movie • Allow users to play the trailer of a movie within the app
  11. In the next video… We’ll go over how exactly we’re

    going to launch a new screen with Android’s Intent
  12. Activities and Intents

  13. Intents When the user taps on a specific movie, we

    want to show them a new screen with more details about the movie
  14. Intents When the user taps on a specific movie, we

    want to show them a new screen with more details about the movie How do we go about launching a new screen?
  15. Intents When the user taps on a specific movie, we

    want to show them a new screen with more details about the movie How do we go about launching a new screen? With an Intent!
  16. Intents An Intent is Android’s way of saying it has

    an ‘intent’ to do something
  17. Intents An Intent is Android’s way of saying it has

    an ‘intent’ to do something Android apps use these Intent objects to specify what it wants to happen (ie : start a new Activity)
  18. Intents An Intent is Android’s way of saying it has

    an ‘intent’ to do something Android apps use these Intent objects to specify what it wants to happen (ie : start a new Activity)
 Intent startNewActivityIntent = new Intent(FirstActivity.this, SecondActivity.class); startActivity(startNewActivityIntent);
  19. Intents An Intent is Android’s way of saying it has

    an ‘intent’ to do something Android apps use these Intent objects to specify what it wants to happen (ie : start a new Activity)
 findViewById(R.id.next_buton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent startNewActivityIntent = new Intent(FirstActivity.this, SecondActivity.class); startActivity(startNewActivityIntent); } });
  20. Intents Now we know how to navigate to a new

    screen! But how do we populate data in the new screen?
  21. Intents Now we know how to navigate to a new

    screen! But how do we populate data in the new screen? When the user clicks on a specific movie, we need to show the details for that specific movie
  22. Intents Now we know how to navigate to a new

    screen! But how do we populate data in the new screen? When the user clicks on a specific movie, we need to show the details for that specific movie This means that when we start the new activity, we also need to pass it some kind of data so it knows how to populate the right information
  23. Intents When an Intent is used, data can also be

    attached to the Intent so that more information can be given to the 'receiver' of the intent.

  24. Intents When an Intent is used, data can also be

    attached to the Intent so that more information can be given to the 'receiver' of the intent.
 Intent startNewActivityIntent = new Intent(FirstActivity.this, SecondActivity.class); startActivity(startNewActivityIntent);
  25. Intents When an Intent is used, data can also be

    attached to the Intent so that more information can be given to the 'receiver' of the intent.
 Intent startNewActivityIntent = new Intent(FirstActivity.this, SecondActivity.class); intent.putExtra(“key", “some info to send”); startActivity(startNewActivityIntent);
  26. Intents FirstActivity Intent intent = new Intent(FirstActivity.this, SecondActivity.class); intent.putExtra(“key", “some

    info to send”); startActivity(intent); SecondActivity protected void onCreate(Bundle savedInstanceState) { … String data = getIntent().getStringExtra(“key"); // data = “some info to send”
  27. Intents FirstActivity Intent intent = new Intent(FirstActivity.this, SecondActivity.class); intent.putExtra(“key", “some

    info to send”); startActivity(intent); SecondActivity protected void onCreate(Bundle savedInstanceState) { … String data = getIntent().getStringExtra(“key"); // data = “some info to send”
  28. Activity’s Lifecycle

  29. Activity’s Lifecycle We can think of each Activity as having

    it’s own ‘lifecycle’
  30. Activity’s Lifecycle We can think of each Activity as having

    it’s own ‘lifecycle’ By observing the different stages of an Activity’s lifecycle, we can know about the state of the Activity and answer questions such as: • Is this the first time we’re showing the user this Activity? • Is our app currently displaying this Activity? • Did the user come back to this Activity after leaving it?

  31. None
  32. None
  33. None
  34. onCreate This is called when the Activity is shown for

    the first time
  35. onCreate This is called when the Activity is shown for

    the first time Since this method is only called the first time an Activity comes into view, it’s the method where we want to do all the necessary setup
  36. onCreate This is called when the Activity is shown for

    the first time Since this method is only called the first time an Activity comes into view, it’s the method where we want to do all the necessary setup
 private TextView helloWorldText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); helloWorldText = findViewById(R.id.helloWorldTv); }
  37. None
  38. onResume This is called when the Activity can be interacted

    with by the user.
  39. onResume This is called when the Activity can be interacted

    with by the user. The Activity remains in this state until the user navigates away from this particular screen (either within our app or outside our app)
  40. onResume This is called when the Activity can be interacted

    with by the user. The Activity remains in this state until the user navigates away from this particular screen (either within our app or outside our app) onResume can be called multiple times for an Activity. It will be called whenever the user comes back to a certain Activity
  41. None
  42. onPause This is called when an Activity is no longer

    in focus (the opposite of onResume)
  43. None
  44. onDestroy This is called when an Activity gets killed and

    cleaned up by the system
  45. onDestroy This is called when an Activity gets killed and

    cleaned up by the system What this means is that if an Activity’s onDestroy() is called, the next time the Activity comes in, onCreate() will get called again
  46. onDestroy This is called when an Activity gets killed and

    cleaned up by the system What this means is that if an Activity’s onDestroy() is called, the next time the Activity comes in, onCreate() will get called again onDestroy can get called in two different ways:
 1) we, the app developer, call finish()
 2) the Android system decides it’s time to kill the Activity
  47. onDestroy findViewById(R.id.cancelBtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {

    finish(); } });
  48. An Example of Activity’s LifeCycle with Intents

  49. An Example of Activity LifeCycles with Intents The scenario: 1)

    We are currently on MainActivity

  50. An Example of Activity LifeCycles with Intents The scenario: 1)

    We are currently on MainActivity 2) We click on the button to go to DetailActivity
  51. An Example of Activity LifeCycles with Intents The scenario: 1)

    We are currently on MainActivity 2) We click on the button to go to DetailActivity 3) We click ‘Back’ and land back on MainActivity
  52. An Example of Activity LifeCycles with Intents The scenario: 1)

    We are currently on MainActivity 2) We click on the button to go to DetailActivity 3) We click ‘Back’ and land back on MainActivity MainActivity DetailActivity onCreate() onResume() not created yet
  53. An Example of Activity LifeCycles with Intents The scenario: 1)

    We are currently on MainActivity 2) We click on the button to go to DetailActivity 3) We click ‘Back’ and land back on MainActivity MainActivity DetailActivity onCreate() onResume() not created yet onPause() onCreate() onResume()
  54. An Example of Activity LifeCycles with Intents The scenario: 1)

    We are currently on MainActivity 2) We click on the button to go to DetailActivity 3) We click ‘Back’ and land back on MainActivity MainActivity DetailActivity onCreate() onResume() not created yet onPause() onCreate() onResume() onResume() finish() onDestroy()