So now that we've created another activity let's see how we go from our original main activity into 1
the display list items activity and also how can we pass data to this activity. Let's keep working with 2
this event handler over here, so if you remember earlier we attach the event handler on click so let's 3
comment the content out. To comment out, 4
all you need to do is select the lines that you are working with press control and Slash. 5
And this will automatically comment out the lines that you're working with let's start with writing 6
code... 7
Over here. 8
So first jump to the new activity where you're going to use an Intent. Intent. 9
I equals new intent. 10
Don't worry about the red warnings for now pass the context and the class of the intent to jump to. Press 11
Alt and Enter everywhere to fix the errors then we're going to get the text from the edit text like 12
we did earlier. 13
So let's do that define a string list item and call this E T dot get text convert this to string. 14
Now we have our list item. 15
Next we're going to pass the string to the new activity by using put extra to do this. 16
We define a variable with the ID of the item to pass we define it as public. 17
In order to access it from the other class then we just put the value, so put extra uses a sort of key 18
value pair you pass the key then the value, once this is done we just start the activity by running start 19
activity. OK now let's set up what happens when the new activity is opened, first thing is to obtain the 20
string that was passed by the original activity, To do this, 21
define a variable called intent and set it to get intent. 22
Next we define a string. 23
Call it item value then we get the string by writing intent. 24
Dot get string extra and pass the ID by pointing to main activity dot extra item which is what we 25
defined earlier. OK. 26
Now let's go to the UI and let's drop a text to view change the idea to text view list item, now in the 27
activity we can refer to the text to view like we did in the previous section so text view TV equals 28
find view by ID, R dot ID dot text view list item then we will change the text of the text view by writing 29
TV dot set text and pass the item value we obtained earlier. OK so let's run this now if we enter text 30
click the button and as you can see we have a new activity opened and the content from the edit text 31
was passed on to the new activity. 32
Another thing we can do is add a back button in order to have the activity go to the previous activity, 33
to do this we edit the manifest and add Android colon parent activity name equals dot main activity 34
and in the metadata we need to add Android colon name equals Android dot support dot parent underscore 35
activity and Android colon value equals dot main activity. 36
Now when we run it again add text click the button and here you can see we have a back button in the 37
activity which sends us back to the original activity.