2010年9月29日星期三

Android - 例子:3个 Activity 之间的数据传输

本实例演示 activity 之间的互相调用和数据传输。

data transmission between Activities.

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();
 }
}


--
yaoms

没有评论:

发表评论