Lesson 3: New Activities and Intents

Udacity – Developing Android Apps

Lesson 3 notes:

  • ItemClickListener and Toast
    • Toast is a pop-up that displays a message a few second before fading out. It uses for debugging because it indicate status visually without altering the app UI.
    • Put listView.setItemClickListener into forecastFragment onCreateView method
    • Add a Toast when the list item is clicked that contains the weather information. Get the forecast by getting an item from the forecast adapter at the position given by the on item click listener.
    • getActivity() vs parent.getContext(). Since Activity extends Context, sometimes when an activity is required, a context must be cast to an activity. getActivity() is available in fragment.
  • Create New Activity
    • Up vs Back. Up button always bring you one level up the hierarchy of the same app but Back button bring you to the last screen that might go across different apps.
    • Create the DetailActivity and look at how the activity is declared in the AndroidManifest file.
    • Intellij: right click on package > New > Activity > Blank Activity with Fragment. Input Activity name and Hierarchical Parent (MainActivity).
  • Intents Framework
    • Use Intent to start activity from MainActivity to DetailActivity
  • Intent as Envelopes
    • Explicit Intent put a message and send to a specified target activity
    • Implicit Intent put a message of what to do and send it out to whoever can do it. This might be another application. Some common intent example are dialing a number, view a website… etc.
  • Launch DetailActivity
    • Replace Toast with an explicit intent to launch the DetailActivity.
    • Pass in weather forecast data for the detail activity to display
    • Create a new instance of Intent with putExtra to include the weather forecast data as EXTRA_TEXT. Use startActivity(intent) to start the activity.
  • Display Content in DetailActivity
    • Read the forecast data from the Intent and display it in DetailActivity
    • Add a text id detail_text at fragment_detail.xml
    • Pull forecastStr from Intent, find the root view and set the text to the forecastStr.
    • List Item Click Listener
      • What method is used to wire up behavior on a list item when it is clicked?
      • setOnItemClickListener (AdapterView.OnItemClickListener listener)
      • ListView Documentation: Register a callback to be invoked when an item in this AdapterView has been clicked.
  • Setting User Experience
    • Setting Developer Guide Document
    • Use PreferenceActivity Class for Gingerbread, PreferenceFragment Class for Honeycone or later.
    • Add SettingsActivity extends from PreferenceActivity and update AndroidManifest to declare SettingsActivity. Make sure the string name has been define in res/strings.xml.
  • Launch SettingsActivity
    • In both MainActivity and DetailActivity, create startActivity with new intent for settingsActivity in onOptionsItemSelected method.
    • create xml res folder and a preference xml file with PreferenceScreen. In EditTextPreference, add title, key and default value as a previously defined string constant.
  • Modify SettingsActivity
    • In onCreate method, addPreferencesFromResource and bindPreferenceSummaryToValue
  • Use SharedPreferences
    • create a SharedPreferences and using PreferenceManager to getDefaultSharedPreferences.
    • create a location string pair of the location key and the default.
    • Use this string for weatherTask when execute
  • Update Date on Activity Start
  • Temperature Units Setting
    • add ListPreference in pref_general.xml file (instead of EditTextPreference). The list of possible value is pass in as an array (entriesValues, entries).
    • refactor to create updateWeather() method that use SharedPreferences for location string
    • override onStart() method to call updateWeather()
    • pass a empty array list in mForecastAdapter instead of fake forecast data
  • Debug Breakpoints
  • Add Map Location Intent
    • add another menu item in main.xml and define the string for the id
    • read preferred location from sharedPreferences
    • create a view intent indicating its location in the data URI. URI format documentation.
    • startActivity with intent.
  • Share Intent
    • First, add the string for action_share
    • Add a new menu resource file with a new item for action_share.
    • Create a private String to store the forecast string in DetailFragment
    • Add a new Intent in DetailFragment. Use FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET to return to the original app and not the app that handle the share intent.
    • Set a flag that this fragment has an option menu under DetailFragment
    • Add onCreateOptionsMenu method by inflat the detailfragment menu, find the share item, getting the ShareActionProvider and attach an intent to the ShareActionProvider.
  • Broadcast Intents
    • Dynamic Receiver only when App running
    • Manifest Receiver will start app specific to receive broadcast.
Advertisements