Saturday, August 20, 2011

Return result to onActivityResult()

To return result to onActivityResult() of calling activity, we can use the method setResult().

main activity
package com.exercise.AndroidResult;


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

public class AndroidResultActivity extends Activity {

final static int REQ_CODE = 1;

TextView tvResultCode;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tvResultCode = (TextView)findViewById(R.id.resultcode);

Button btnStartActivity2 = (Button)findViewById(R.id.startactivity2);
btnStartActivity2.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(AndroidResultActivity.this, Activity2.class);
startActivityForResult(intent, REQ_CODE);
}});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == REQ_CODE){
if (resultCode == RESULT_OK){
tvResultCode.setText("RESULT_OK");
}else if(resultCode == RESULT_CANCELED){
tvResultCode.setText("RESULT_CANCELED");
}
}
}
}


main.xml
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startactivity2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Activity 2"
/>
<TextView
android:id="@+id/resultcode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Sub-activity
package com.exercise.AndroidResult;


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

public class Activity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);

Button btnReturnOK = (Button)findViewById(R.id.returnOK);
btnReturnOK.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
setResult(RESULT_OK, i);
finish();
}});

Button btnReturnCancel = (Button)findViewById(R.id.returnCancel);
btnReturnCancel.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
setResult(RESULT_CANCELED, i);
finish();
}});

}

}


<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/returnOK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="return OK"
/>
<Button
android:id="@+id/returnCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="return Cancel"
/>
</LinearLayout>


No comments: