Last active
June 8, 2016 00:24
-
-
Save ceruleanotter/06d798b217d29978c0473a3963aab896 to your computer and use it in GitHub Desktop.
Intent Extras To Send Data Between Activities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the code showing how to get user input from and EditText | |
// First, use findViewById to get the EditText view (just like you would | |
// get any other view in the layout | |
EditText myEditText = (EditText) findViewById(R.id.send_edit_text); | |
// Then from the edit text, perform two actions, first to get the "Text", then | |
// to change it to a String. It is kind of silly that you can't just have one | |
// action "getString", but it is the way that the engineers of Android chose | |
// to make it so ¯\_(ツ)_/¯ | |
String userInput = myEditText.getText().toString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code create an Intent. An intent is like an envelope. This specifies where | |
// the envelope is going. | |
// You have seen this code before when just opening a new activity | |
Intent intent = new Intent(this, OtherActivity.class); | |
// This adds some extra data to the intent. Using the envelope analogy | |
// this is like putting something (a letter, a card) into the envelope | |
// for the recieving activity | |
// The key will always be a String - it is like the "Name" | |
// of the Extra that you give. In this example the key is "KeyExample" | |
// The value can be pretty much any primitive value, or a set of strings | |
// In this example the value is "ValueExample" | |
// You will use the key on the other side to get the value. | |
intent.putExtra("KeyExample", "ValueExample"); | |
// This code you have seen before. It starts the new activity. | |
startActivity(intent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the intent that caused this activity to start. In the mailing analogy | |
// this is like picking up for mail. | |
Intent startingIntent = getIntent(); | |
// Get the String Extra from the intent using the key "KeyExample" | |
// Remember, when we sent the intent message, we put one Extra | |
// value, and it's key was spelled exactly "KeyExample". This will retrieve | |
// that value from the intent. In the analogy, this is like opening | |
// the envelope to receive the letter. | |
// Notice also how the method we call is getStringExtra. This is because | |
// the value being sent it a String. | |
String sentData = startingIntent.getStringExtra("KeyExample"); | |
// This sets the text view to show the data that was sent. It will show the | |
// text "ValueExample" in this example | |
informationTextView.setText("The value sent is " + sentData); | |
// In this example, we want to output the value for the user to see. | |
// To do that, we first get the textview we are going to ouput the value in. | |
// You've seen this before. | |
TextView informationTextView = (TextView) findViewById(R.id.information_text_view); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment