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

Android University - Week 2

Caren
December 07, 2018
360

Android University - Week 2

Caren

December 07, 2018
Tweet

Transcript

  1. 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

  2. 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)

  3. 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) { } }
  4. 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) { } }
  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) { 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) { } }
  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 { } // 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()); } }
  7. 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
  8. In the next video… We’ll go over how exactly we’re

    going to launch a new screen with Android’s Intent
  9. Intents When the user taps on a specific movie, we

    want to show them a new screen with more details about the movie
  10. 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?
  11. 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!
  12. 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)
  13. 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);
  14. 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); } });
  15. Intents Now we know how to navigate to a new

    screen! But how do we populate data in the new screen?
  16. 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
  17. 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
  18. 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.

  19. 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);
  20. 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);
  21. 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”
  22. 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”
  23. 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?

  24. 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
  25. 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); }
  26. 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)
  27. 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
  28. onPause This is called when an Activity is no longer

    in focus (the opposite of onResume)
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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()
  35. 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()