Activity 끼리 Data 전달하기
Activity A -> Activity B (data 입력) -> Activity A (data receive and write to text box)
ActivitySwitch mani
<?xml version=”1.0” encoding=”utf-8”?>
ActivitySwitch.java
package com.sulac.ActivitySwitch;
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 ActivitySwitch extends Activity {
private final int REQUEST_CODE = 1;
Button btn;
TextView tv;
/-* Called when the activity is first created. *-
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.buttonA);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent =new Intent(ActivitySwitch.this, NewActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
String str = data.getStringExtra(“data_name”);
tv = (TextView)findViewById(R.id.TextViewA);
tv.setText(str);
}
}
}
}
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=”—–A—–”
/>
<TextView
android:id=”@+id/TextViewA”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>
<Button
android:id=”@+id/buttonA”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”go B”
/>
</LinearLayout>
NewActivity.java
package com.sulac.ActivitySwitch;
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 NewActivity extends Activity {
Button btn;
EditText et;
/-* Called when the activity is first created. *-
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testswitch);
btn = (Button)findViewById(R.id.buttonB);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
et = (EditText)findViewById(R.id.EditText01);
Intent intent = getIntent();
intent.putExtra(“data_name”, et.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
});
}
}
testswitch.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=”—–B—–”
/>
<EditText
android:id=”@+id/EditText01”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>
<Button
android:id=”@+id/buttonB”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”go A”
/>
</LinearLayout>
\