Home > Android, Android how to > Using SubActivities in Android

Using SubActivities in Android


We moved. Please visit this link for this post.

Sometimes you need to run some Activity, do some operation there and return some value to parent Activity. For this situation Android has possibility to run Activity for result.

I am going to show how to use it.

There are 3 things which you should do:

1) you need to run your subactivity via this method

startActivityForResult(Intent i, int requestCode);

2) in parent activity you should override method

onActivityResult(int requestCode, int resultCode, Intent data)

3) in subactivity you need to put result and finish this activity

setResult(int resultCode, Intent data);

finish();

As you see it is simple.

Now I am going to show whole example:

First off all need to create Parent (Main) activity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Parent activty
 *
 * @author FaYnaSoft Labs
 */
public class Main extends Activity {
	protected static final int SUB_ACTIVITY_REQUEST_CODE = 100;

	TextView tv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		tv = (TextView) findViewById(R.id.textId);
		tv.setText("TextView element");
		Button b = (Button) findViewById(R.id.btnId);
		b.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(Main.this,
						SubActivity.class);
				startActivityForResult(i, SUB_ACTIVITY_REQUEST_CODE);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == SUB_ACTIVITY_REQUEST_CODE){
				Bundle b = data.getExtras();
				tv.setText(b.getString("TEXT"));
        }
	}
}

As you see here we start activity for result and we override onActivityResult method.
Also simple main layout:

<?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">

 <TextView android:id="@+id/textId" android:layout_width="wrap_content"

 android:layout_height="wrap_content" />

 <Button android:id="@+id/btnId" android:layout_width="wrap_content"

 android:layout_height="wrap_content" android:text="Click" />

</LinearLayout>

Now we need sub activity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SubActivity extends Activity {
	public final static int SUCCESS_RETURN_CODE = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.subactivity);
		final EditText editView = (EditText) findViewById(R.id.editId);
		editView.setText("SubActivity");
		Button button = (Button) findViewById(R.id.btnId);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle b = new Bundle();
				b.putString("TEXT", editView.getText().toString());
				intent.putExtras(b);
				setResult(SUCCESS_RETURN_CODE, intent);
				finish();
			}
		});
	}
}

Here we put result for main activity.
Also layout for subactivity

<?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 android:id="@+id/editId" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/btnId" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Click" />
</LinearLayout>

As you see it is very easy to get data from sub activity.

Is it useful post for you? Leave a comment.

About these ads
Categories: Android, Android how to
  1. December 10, 2009 at 12:07 am | #1

    Dear Author thedevelopersinfo.wordpress.com !
    Thanks for the valuable information. I have used it.

  2. April 14, 2010 at 5:45 pm | #3

    Please check this code:
    public void onClick(View v) {
    Intent i = new Intent(Main.this,
    SubActivityWithResult.class);
    startActivityForResult(i, SUB_ACTIVITY_REQUEST_CO

    Where is SubActivityWithResult named?
    Also, where is the subactivity layout?

    • April 14, 2010 at 7:58 pm | #4

      Hello David.
      Thanks for your attention and for this bug. I have fixed the code. There is no SubActivityWithResult, there is SubActivity class.
      Also in this post I use the same layout as for Main activity (last snippet of code).

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: