본문 바로가기

Android/startActivityForResult

Android startActivityForResult method example

Android 에서 3개의 Activity간에 데이터 전달 예제


IntentTestActivity --> SecondActivity --> ThirdActivity --> SecondActivity --> IntentTestActivity에서 출력

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:id="@+id/textView"
     android:layout_width="fill_parent"
     android:layout_height="70px"/>
</LinearLayout>


 

package com.dearpeople.android.test.intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class IntentTestActivity extends Activity {
   
 TextView textView;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView)this.findViewById(R.id.textView);
       
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra("sampleData", "This is Sample Data");
        startActivityForResult(intent, 1);
    }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if(resultCode==RESULT_OK && requestCode==1){
   String msg = data.getStringExtra("returnedData");
   textView.setText(msg);
  }
 }
}



 

package com.dearpeople.android.test.intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class SecondActivity extends Activity {

 Intent intent;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  intent= getIntent();
  String msg = intent.getStringExtra("sampleData");
  msg += ", Added at Second";
 
  Intent intent2 = new Intent(this, ThirdActivity.class);
  intent2 .putExtra("sampleData", msg);
  startActivityForResult(intent2, 10);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if(resultCode==RESULT_OK && requestCode==10){
   String msg = data.getStringExtra("returnedData");
   intent.putExtra("returnedData", msg);
   setResult(RESULT_OK, intent);
   finish();
  }
 }
}



 

package com.dearpeople.android.test.intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class ThirdActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Intent intent= getIntent();
  String msg = intent.getStringExtra("sampleData");
  msg += ", Added at Third";
  intent.putExtra("returnedData", msg);
  setResult(RESULT_OK, intent);
  finish();
 }
}