I’m new to Android. A part of my Activity contains
TextView message = (TextView) findViewById(R.id.show_message);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
message.setText(name);
MainActivity was the previous activity from which the current one was called. Also, the layout file for the above activity is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/show_message"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content">
</TextView>
<Button
android:id="@+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/show_message"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:onClick="goBack"
android:text="Go Back to Activity Lifecycle" />
I know that message.setText(name) sets the String value of name to message but how come it also changes show_message? When the Activity starts in the app, it shows the String value of name where it is supposed to display show_message.
Actually, I want the code to perform in the way it is doing right now but I can’t understand why.
Thanks in advance!
The value of
String nameisn’t being set bymessage.setTextrather it’s being set by the StringExtra in the received intent (that starts the activity)TextView messageis pointing to theTextView R.id.show_messageSo
message.setText("anything")would set the text value of show_message to “anything”message.setText(name)sets the text of show_message to whatever String name represents… here the value passed in the intent.