SubActivites with return value - The InputBox
What you learn: You will learn how to start a SubActivity, that will return a String (+ extra Data), what is in the end exactly like an InputBox.
Difficulty: 1 of 5

What it will look like:

Description
This has been a very common question, as in "normal" Java you can make a blocking a MessageBox was displayed with code like:
Using java Syntax Highlighting
- public static String JOptionPane.showInputDialog(Component parentComponent, Object message);
- // Example implementation:
- String retString = JOptionPane.showInputDialog(this,"Enter your name:");
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
in Android you can't really do fully blocking Dialogs, imagine what should happen if there is an incoming phone-call
showAlert, will not help us, as it is a non-blocking dialog. Remember:
Using java Syntax Highlighting
- showAlert("A funny title", "MessageBoxes rule extremely!", "Hit Me!", false);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
But there is a pretty easy workaround for doing so...
0. Lets quickly define the Strings we are going to use
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="main_app_name">MainActivity - anddev.org</string>
- <string name="main_cmd_opensub">Open SubActivity</string>
- <string name="sub_app_name">SubActivity returning Result - anddev.org</string>
- <string name="subactivity_cmd_return">Return to MainActivity</string>
- </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
1. What we need to do is the follwoing:
- Override the onActivityResult(...)-method coming from Activity to react on a Activity we opened, that returned sth. .
- Start that Activity that will return sth. to us, using: startSubActivity(...)
- Call setResult(int resultCode, String data) within our (sub)Activity and close it.
2. So this is the layout-file of the MainActivity (we simply need a button, nothing more):
We need to add the id-attribute to the Button, because we need to add an OnClickListener to it.
For basic information about XML-Layouting, take a look :arrow: here.
"/res/layout/main.xml"
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button id="@+id/mainactivity_cmd_opensub"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/main_cmd_opensub"
- />
- </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
"/src/your_package_structure/MainActivity.java"
Using java Syntax Highlighting
- package org.anddev.android.subactivitywithresult;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- protected static final int SUB_ACTIVITY_REQUEST_CODE = 1337;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- // Apply our main.xml-Layout
- setContentView(R.layout.main);
- // Find the button defined in the main.xml
- Button cmd_opensub = (Button)findViewById(R.id.mainactivity_cmd_opensub);
- // Add an OnClickListener to it, that will open the SubActivity
- cmd_opensub.setOnClickListener(new OnClickListener(){
- // @Override
- public void onClick(View arg0) {
- Intent i = new Intent(MainActivity.this,
- SubActivityWithResult.class);
- // We use SUB_ACTIVITY_REQUEST_CODE as an 'identifier'
- startSubActivity(i, SUB_ACTIVITY_REQUEST_CODE);
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode,
- String data, Bundle extras) {
- super.onActivityResult(requestCode, resultCode, data, extras);
- // Here We identify the subActivity we starte
- if(requestCode == SUB_ACTIVITY_REQUEST_CODE){
- // And show its result
- showAlert("SubActivity returned", "ReturnValue: " + data,
- "OK", false);
- }
- }
- }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
3.Now lets take a look at the SubActivity.xml (provides an EditText to enter the return-String and a "Return to Main"-Button and the SubActivityWithResult-Class, which is also well commented:
"/res/layout/subactivity.xml"
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText id="@+id/subactivity_edit_returnvalue"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@android:drawable/editbox_background"
- android:singleLine="true"
- />
- <Button id="@+id/subactivity_cmd_return"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/subactivity_cmd_return"
- />
- </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
"/src/your_package_structure/SubActivityWithResult.java"
Using java Syntax Highlighting
- package org.anddev.android.subactivitywithresult;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class SubActivityWithResult extends Activity{
- protected final int SUCCESS_RETURN_CODE = 1;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- // Apply our subactivity.xml-Layout
- setContentView(R.layout.subactivity);
- // Find the button defined in the subactivity.xml
- Button cmd_return = (Button)findViewById(R.id.subactivity_cmd_return);
- /* Add an OnClickListener to it, that will
- * read out the text in the EditBox set it
- * as retrn-data and close this activity */
- cmd_return.setOnClickListener(new OnClickListener(){
- // @Override
- public void onClick(View arg0) {
- // Find the edittext defined in the subactivity.xml
- EditText edit_returvalue = (EditText)findViewById(
- R.id.subactivity_edit_returnvalue);
- SubActivityWithResult.this.setResult(SUCCESS_RETURN_CODE,
- edit_returvalue.getText().toString());
- // Close this Activity
- SubActivityWithResult.this.finish();
- }
- });
- }
- }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
4. And finally the AndroidManifets.xml which was has to be altered like the following:
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="org.anddev.android.subactivitywithresult">
- <application android:icon="@drawable/icon">
- <activity class=".MainActivity" android:label="@string/main_app_name">
- <intent-filter>
- <action android:value="android.intent.action.MAIN" />
- <category android:value="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity class=".SubActivityWithResult" android:label="@string/sub_app_name">
- <intent-filter>
- <action android:value="android.intent.action.VIEW" />
- <category android:value="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
Regards,
plusminus




is it because the Intent is served from the Activity class while its in the ListView that the Intent is received.



