Using SubActivities in Android

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 can see it is simple.

Now I am going to show full 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 activity
 *
 * @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 can 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 can see it is very easy to use sub activity in Android.

Is it useful post for you? Leave a comment.

 

If you’d like to get the latest posts as soon as they’re published, subscribe to our feed!

  • Share/Bookmark

Related posts:

  1. Making not editable EditText component in Android
  2. Passing data between activities in Android
  3. Using Sound Recorder activity for capturing audio in Android
  4. Getting formatted string from strings.xml in Android
  5. Email sending in Android

2 comments to Using SubActivities in Android

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>