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

Android University - Week 4

Caren
January 07, 2019
170

Android University - Week 4

Caren

January 07, 2019
Tweet

Transcript

  1. (Last Week) Twitter : Part 1 - User can sign

    in to Twitter using OAuth login - User can view the tweets from their home timeline - User can refresh tweets timeline by pulling down to refresh
  2. Twitter: Part 2 - User can click a "Compose" icon

    to post a new Tweet in a separate Activity - When navigating back to the Home activity, user can see the new Tweet they just posted
  3. Twitter: Part 2 - User can click a "Compose" icon

    to post a new Tweet in a separate Activity -> AppBar - When navigating back to the Home activity, user can see the new Tweet they just posted -> 
 Intents and startActivityForResult()
  4. AppBar : Adding Action Items 1. Create menu resource file

    (design) 2. Override onCreateOptionsMenu() to inflate custom menu
  5. AppBar : Adding Action Items 1. Create menu resource file

    (design) 2. Override onCreateOptionsMenu() to inflate custom menu 3. Override onOptionsItemSelected() to handle clicks
  6. 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”
  7. We’ve been starting new Activities with something like:
 Intent intent

    = new Intent(MainActivity.this, DetailyActivity.class); startActivity(intent); Returning Data Result to Parent Activity
  8. We’ve been starting new Activities with something like:
 Intent intent

    = new Intent(MainActivity.this, DetailyActivity.class); startActivity(intent); But now that we want information back from the Activity we launched, we can start the new Activity like so:
 Intent intent = new Intent(MainActivity.this, DetailyActivity.class); startActivityForResult(intent, LAUNCH_DETAIL_REQUEST_CODE); private static final int LAUNCH_DETAIL_REQUEST_CODE = 100; 
 Returning Data Result to Parent Activity
  9. We’ve been starting new Activities with something like:
 Intent intent

    = new Intent(MainActivity.this, DetailyActivity.class); startActivity(intent); But now that we want information back from the Activity we launched, we can start the new Activity like so:
 Intent intent = new Intent(MainActivity.this, DetailyActivity.class); startActivityForResult(intent, LAUNCH_DETAIL_REQUEST_CODE); private static final int LAUNCH_DETAIL_REQUEST_CODE = 100; 
 Returning Data Result to Parent Activity
  10. Returning Data Result to Parent Activity 1) MainActivity starts ComposeActivity

    with
 startActivityForResult(intent, 100); 2) User is taken to ComposeActivity
  11. Returning Data Result to Parent Activity 1) MainActivity starts ComposeActivity

    with
 startActivityForResult(intent, 100); 2) User is taken to ComposeActivity 3) User does some stuff in ComposeActivity, and then closes ComposeActivity Intent intent = new Intent(); intent.putExtra("newTweet", "A Tweet!"); setResult(RESULT_OK, intent); finish(); // closes the activity, pass data to parent
  12. Returning Data Result to Parent Activity 1) MainActivity starts ComposeActivity

    with
 startActivityForResult(intent, 100); 2) User is taken to ComposeActivity 3) User does some stuff in ComposeActivity, and then closes ComposeActivity. Intent intent = new Intent(); intent.putExtra("newTweet", "A Tweet!"); setResult(RESULT_OK, intent); finish(); // closes the activity, pass data to parent 4) MainActivity’s onActivityResult() is called immediately when the user returns @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == 100) { // this means we just came back from ComposeActivity! } }
  13. Returning Data Result to Parent Activity 1) MainActivity starts ComposeActivity

    with
 startActivityForResult(intent, 100);; 2) User is taken to ComposeActivity 3) User does some stuff in ComposeActivity, and then closes ComposeActivity. Intent intent = new Intent(); intent.putExtra("newTweet", "A Tweet!"); setResult(RESULT_OK, intent); finish(); // closes the activity, pass data to parent 4) MainActivity’s onActivityResult() is called immediately when the user returns @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == 100) { // this means we just came back from ComposeActivity! } }
  14. Returning Data Result to Parent Activity 1) MainActivity starts ComposeActivity

    with
 startActivityForResult(intent, 100); 2) User is taken to ComposeActivity 3) User does some stuff in ComposeActivity, and then closes ComposeActivity. Intent intent = new Intent(); intent.putExtra("newTweet", "A Tweet!"); setResult(RESULT_OK, intent); finish(); // closes the activity, pass data to parent 4) MainActivity’s onActivityResult() is called immediately when the user returns @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == 100) { String newTweetToInsert = data.getExtras().getString("newTweet"); } }