What you learn: You will learn how to pass Data to Activities using Intent.putExtras(Bundle).
Difficulty: 1 of 5
SubActivites with return value - The InputBox.
What it will look like:
[align=center]
[/align]
Description:
0.) We are going to extend the
SubActivites with return value - The InputBox-Tutorial with a Feature, that allows us to add some data to the Intent that start SubActivity form within the MainActivity. In this case we pass a String to the SubActivity, that uses the Intent as a "Carrier", that will be set to the Defaulttext of the SubActivity-EditText.
1.) In the MainActivity we add a Bundle to the Intent that will start the SubActivity. A Bundle is more or less a HashMap (Associates Objects with String-Keys) with a lot of specialized put/get-methods.
Using java Syntax Highlighting
- // ...
- cmd_opensub.setOnClickListener(new OnClickListener(){
- // @Override
- public void onClick(View arg0) {
- Intent i = new Intent(MainActivity.this,
- SubActivityWithResult.class);
- /* Create a bundle that will
- * hold will be passed to the
- * SubActivityover the Intent */
- Bundle b = new Bundle();
- b.putString("DEFAULTTEXT", "Some Default blabla...");
- i.putExtras(b);
- // We use SUB_ACTIVITY_REQUEST_CODE as an 'identifier'
- startSubActivity(i, SUB_ACTIVITY_REQUEST_CODE);
- }
- });
- // ...
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
2.) In the SubActivitiy we just grab the Bundle from the Intent that started it and read out the String-Value using the same KEY.
Using java Syntax Highlighting
- // ...
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- // Apply our subactivity.xml-Layout
- setContentView(R.layout.subactivity);
- /* Retrieve the String from the Extra-Bundle
- * we started this SubActivity with. */
- Bundle b = this.getIntent().getExtras();
- String s = b.getString("DEFAULTTEXT");
- // Find the EditText defined in the subactivity.xml
- ((EditText)findViewById(R.id.subactivity_edit_returnvalue)).setText(s);
- // ...
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
[align=center]Thats it
[/align]
Regards,
plusminus








