a list of currently playing movies (Activity, RecyclerView) • Each movie item has info including title, image, and overview (Layouts, ImageViews, network calls)
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) { } }
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) { } }
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) { } }
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()); } }
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
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);
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); } });
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
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);
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);
info to send”); startActivity(intent); SecondActivity protected void onCreate(Bundle savedInstanceState) { … String data = getIntent().getStringExtra(“key"); // data = “some info to send”
info to send”); startActivity(intent); SecondActivity protected void onCreate(Bundle savedInstanceState) { … String data = getIntent().getStringExtra(“key"); // data = “some info to send”
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?
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); }
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)
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
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
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
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
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()
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()