Last active
August 29, 2015 14:06
-
-
Save MaksimDmitriev/e6c4c365c389805c257d to your computer and use it in GitHub Desktop.
Set text of a TextView programmatically and show it after the screen roitation
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.mock_android" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="8" | |
android:targetSdkVersion="21" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name=".MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<activity android:name="com.example.mock_android.SecondAct" > | |
</activity> | |
</application> | |
</manifest> |
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
public class MainActivity extends Activity { | |
private static final int SECOND_ACT_REQUEST_CODE = 10; | |
private TextView mTextView1; | |
private TextView mTextView2; | |
public static final String TEXT_1 = "TEXT_1"; | |
public static final String TEXT_2 = "TEXT_2"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Log.d("Mock", MainActivity.class.getSimpleName() + ".onCreate"); | |
setContentView(R.layout.activity_main); | |
mTextView1 = (TextView) findViewById(R.id.text1); | |
mTextView2 = (TextView) findViewById(R.id.text2); | |
findViewById(R.id.start_second).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
startActivityForResult(new Intent(getApplicationContext(), SecondAct.class), | |
SECOND_ACT_REQUEST_CODE); | |
} | |
}); | |
if (savedInstanceState != null) { | |
setTexts(savedInstanceState); | |
} | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
outState.putString(TEXT_1, mTextView1.getText().toString()); | |
outState.putString(TEXT_2, mTextView2.getText().toString()); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
switch (requestCode) { | |
case SECOND_ACT_REQUEST_CODE: | |
switch (resultCode) { | |
case RESULT_OK: | |
if (data != null) { | |
setTexts(data.getExtras()); | |
} | |
break; | |
default: | |
break; | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
private void setTexts(Bundle extras) { | |
mTextView1.setText(extras.getString(TEXT_1)); | |
mTextView2.setText(extras.getString(TEXT_2)); | |
} | |
} |
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
public class SecondAct extends Activity { | |
private EditText mDoneEt; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_sec); | |
mDoneEt = (EditText) findViewById(R.id.edit_text); | |
findViewById(R.id.done).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent goBack = new Intent(); | |
Bundle bundle = new Bundle(); | |
bundle.putString(MainActivity.TEXT_1, mDoneEt.getText().toString() + " 1"); | |
bundle.putString(MainActivity.TEXT_2, mDoneEt.getText().toString() + " 2"); | |
goBack.putExtras(bundle); | |
setResult(RESULT_OK, goBack); | |
finish(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment