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()
info to send”); startActivity(intent); SecondActivity protected void onCreate(Bundle savedInstanceState) { … String data = getIntent().getStringExtra(“key"); // data = “some info to send”
= 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
= 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
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
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! } }
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! } }
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"); } }