2012年4月18日水曜日

Activityの呼び出し(4)

アクティビティに値を渡す方法は前回紹介しましたので、今回はその逆、呼び出したアクティビティから値を受け取る方法を紹介します。

呼び出し元は、前々回の「Activityの呼び出し(2)」と同じく、startActivityForResult()とonActivityResult()を使います。

いつもとは順番が違いますが、まず呼び出される側のソースから。

SubActivity.java
package jp.co.triware.samples.ActivityTest4a;

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

public class SubActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sub);

        Intent intent = getIntent();
        String pkg = getPackageName();
        String buf = intent.getStringExtra(pkg + ".input");
        EditText et = (EditText)findViewById(R.id.text_et);
        et.setText(buf);

        Button btnClose = (Button)findViewById(R.id.close_btn);
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et = (EditText)findViewById(R.id.text_et);
                String buf = et.getText().toString();
                Intent intent = new Intent();
                String pkg = getPackageName();
                intent.putExtra(pkg + ".return", buf);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

ボタンがクリックされたら、テキストを取得して(26-27行目)、インテントにセットします(28-30行目)。インテントはsetResult()の第2引数に指定して、呼び出し元へ引き渡します(31行目)。

ActivityTest4aActivity.java
package jp.co.triware.samples.ActivityTest4a;

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

public class ActivityTest4aActivity extends Activity {
    private static final int REQUEST_SUB_ACTIVITY = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btnOpen = (Button)findViewById(R.id.open_btn);
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pkg = getPackageName();
                EditText et = (EditText)findViewById(R.id.text_et);
                String buf = et.getText().toString();
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), SubActivity.class);
                intent.putExtra(pkg + ".input", buf);
                startActivityForResult(intent, REQUEST_SUB_ACTIVITY);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_SUB_ACTIVITY  && resultCode == RESULT_OK) {
            String pkg = getPackageName();
            String buf = data.getStringExtra(pkg + ".return");
            Toast.makeText(getApplicationContext(), buf, Toast.LENGTH_LONG).show();
        }
    }
}
呼び出し元です。onActivityResult()の第2引数には、setResult()の第1引数にセットされた「RESULT_OK」が入っています。onActivityResult()の第3引数には、setResult()の第2引数でセットされたインテントが入っています。このインテントからキーワードを指定して値を取得します。

レイアウトファイルとマニフェストファイルは、これまでと同様です。

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[Main Activity] Please input your message:"
        />
    <EditText
        android:id="@+id/text_et"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        />
    <Button
        android:id="@+id/open_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="OPEN"
        />
</LinearLayout>

sub.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[Sub Activity] Received message:"
        />
    <EditText
        android:id="@+id/text_et"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        />
    <Button
        android:id="@+id/close_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="CLOSE"
        />
</LinearLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jp.co.triware.samples.ActivityTest4a"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ActivityTest4aActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".SubActivity" />
    </application>

</manifest>

実行結果
MainActivityで入力したテキストが、SubActivityで表示されています。ここまでは前回と同じですね。

SubActivtyで入力したテキストを、MainActivityで受け取ってトースト表示しています。

0 件のコメント:

コメントを投稿