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

Android University - Week 4

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=47 Caren
January 07, 2019
150

Android University - Week 4

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=128

Caren

January 07, 2019
Tweet

Transcript

  1. Android University Week 4: Twitter (Part 2)

  2. (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
  3. 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
  4. 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()
  5. AppBar

  6. AppBar

  7. AppBar - navigation element standard throughout modern Android applications

  8. AppBar

  9. AppBar : Adding Action Items

  10. AppBar : Adding Action Items 1. Create menu resource file

    (design)
  11. AppBar : Adding Action Items 1. Create menu resource file

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

    (design) 2. Override onCreateOptionsMenu() to inflate custom menu 3. Override onOptionsItemSelected() to handle clicks
  13. Returning Data Result to Parent Activity

  14. 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”
  15. 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
  16. 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
  17. 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
  18. Returning Data Result to Parent Activity 1) MainActivity starts ComposeActivity

    with
 startActivityForResult(intent, 100);
  19. Returning Data Result to Parent Activity 1) MainActivity starts ComposeActivity

    with
 startActivityForResult(intent, 100); 2) User is taken to ComposeActivity
  20. 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
  21. 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! } }
  22. 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! } }
  23. 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"); } }