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.
Dear Author thedevelopersinfo.wordpress.com !
Thanks for the valuable information. I have used it.
Hi.
I am glad that this information is helpful for you
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?
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).