end0tknr's kipple - 新web写経開発

http://d.hatena.ne.jp/end0tknr/ から移転します

インテント入門(写経)

基本的な画面遷移

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jp.end0tknr.intenttest"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <!--第1(main)画面 activity class名を指定  -->
        <activity
            android:name=".IntentTestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--第2画面 activity class名を指定  -->
        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" />
    </application>
</manifest>
res/layout/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" >
    
<Button android:id="@+id/tosecond"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="第二の画面に進む" />
</LinearLayout>
res/layout/second.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:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="私は第二の画面です。" />
</LinearLayout>
src/IntentTestActivity.java (第1(メイン)画面)
package jp.end0tknr.intenttest;

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

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

        //イベントリスナー登録
        final Button tosecond = (Button) findViewById(R.id.tosecond);
        tosecond.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                startSecondActivity();
            }
        });
    }
    
    //第2画面の呼出
    private void startSecondActivity() {
        Intent i = new Intent(this, SecondActivity.class);
        // http://developer.android.com/reference/android/app/Activity.html
        //第2引数: int resultCode
        startActivityForResult(i, 0);
    }
}
src/SecondActivity.java (第2画面)
package jp.end0tknr.intenttest;

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second); // refer to second.xml
    }
}

putExtra(),getExtras()によるアクティビティ間でのデータ授受

構造化されたデータの受け渡しはどうするんだろう?

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jp.end0tknr.intenttest2"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <!--第1(main)画面 activity classを指定  -->
        <activity
            android:name=".IntentTest2Activity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--第2画面 activity classを指定  -->
        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name"/>
    </application>
</manifest>
res/layout/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" >

<EditText android:id="@+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText android:id="@+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText android:id="@+id/edittext3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/tosecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二の画面に進む"
/>

</LinearLayout>
res/layout/second.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:id="@+id/textview1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/textview2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/textview3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>
src/IntentTest2Activity.java
package jp.end0tknr.intenttest2;

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

public class IntentTest2Activity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button tosecond = (Button) findViewById(R.id.tosecond);
        tosecond.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                startSecondActivity();
            }
        });
    }
    
    private void startSecondActivity() {
        final EditText edittext1 = (EditText) findViewById(R.id.edittext1);
        final EditText edittext2 = (EditText) findViewById(R.id.edittext2);
        final EditText edittext3 = (EditText) findViewById(R.id.edittext3);
        String s1 = edittext1.getText().toString();
        String s2 = edittext2.getText().toString();
        String s3 = edittext3.getText().toString();
        Intent i = new Intent(this, SecondActivity.class);
        i.putExtra("edittext1_val", s1);
        i.putExtra("edittext2_val", s2);
        i.putExtra("edittext3_val", s3);
        startActivityForResult(i, 0);
    }
}
src/SecondActivity.java
package jp.end0tknr.intenttest2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        final TextView textview1 = (TextView) findViewById(R.id.textview1);
        final TextView textview2 = (TextView) findViewById(R.id.textview2);
        final TextView textview3 = (TextView) findViewById(R.id.textview3);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String s1 = extras.getString("edittext1_val");
            String s2 = extras.getString("edittext2_val");
            String s3 = extras.getString("edittext3_val");
            textview1.setText(s1);
            textview2.setText(s2);
            textview3.setText(s3);
        }
    }
}

setResult() finish() による他アクティビティの結果の取得

AndroidManifest.xml

合計3画面(activity)があります

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".IntentTest3Activity"
            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=".SecondActivity"
            android:label="@string/app_name" />
        <activity
            android:name=".ThirdActivity"
            android:label="@string/app_name" />
    </application>
</manifest>
res/layout/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" >

<Button
    android:id="@+id/tosecond"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="第二の画面に進む" />

<Button android:id="@+id/tothird"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="第三の画面に進む" />

<TextView android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>
res/layout/second.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" >

<Button
    android:id="@+id/second_ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="OK" />

<Button
    android:id="@+id/second_cancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="キャンセル" />

</LinearLayout>
res/layout/third.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" >
<EditText
    android:id="@+id/edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/third_ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="OK" />
<Button
    android:id="@+id/third_cancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="キャンセル" />
</LinearLayout>
src/IntentTest3Activity.java
package jp.end0tknr.intenttest3;

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

public class IntentTest3Activity extends Activity {
    private static final int SECOND_ACTIVITY = 2;
    private static final int THIRD_ACTIVITY = 3;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button tosecond = (Button) findViewById(R.id.tosecond);
        tosecond.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                startSecondActivity();
            }
        });
        final Button tothird = (Button) findViewById(R.id.tothird);
        tothird.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                startThirdActivity();
            }
        });
    }
    @Override
    public void onActivityResult(
            int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        setMessage("");
        String message = "";
        switch (requestCode) {
        case SECOND_ACTIVITY:
            message = "隨ャ莠後&#65533;逕サ髱「:";
            switch (resultCode) {
            case Activity.RESULT_OK:
                message += "RESULT_OK";
                break;
            case Activity.RESULT_CANCELED:
                message += "RESULT_CANCELED";
                break;
            }
            break;
        case THIRD_ACTIVITY:
            message = "隨ャ荳峨&#65533;逕サ髱「:";
            switch (resultCode) {
            case Activity.RESULT_OK:
                message += "RESULT_OK:";
                Bundle extras = data.getExtras();
                message += extras.getString("org.example.result.s");
                break;
            case Activity.RESULT_CANCELED:
                message += "RESULT_CANCELED";
                break;
            }
            break;
        }
        setMessage(message);
    }
    private void startSecondActivity() {
        Intent i = new Intent(this, SecondActivity.class);
        startActivityForResult(i, SECOND_ACTIVITY);
    }
    private void startThirdActivity() {
        Intent i = new Intent(this, ThirdActivity.class);
        startActivityForResult(i, THIRD_ACTIVITY);
    }
    private void setMessage(String message) {
        final TextView textview = (TextView) findViewById(R.id.textview);
        textview.setText(message);
    }
}
src/SecondActivity.java
package jp.end0tknr.intenttest3;

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

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        final Button ok = (Button) findViewById(R.id.second_ok);
        
        ok.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                setResult(Activity.RESULT_OK);
                finish();
            }
        });
        
        final Button cancel = (Button) findViewById(R.id.second_cancel);
        cancel.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                setResult(Activity.RESULT_CANCELED);
                finish();
            }
        });
    }
}
src/ThirdActivity.java
package jp.end0tknr.intenttest3;

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

public class ThirdActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third);
        final Button third_ok = (Button) findViewById(R.id.third_ok);
        third_ok.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                setResultOfString();
                finish();
            }
        });
        final Button third_cancel = (Button) findViewById(R.id.third_cancel);
        third_cancel.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                setResult(Activity.RESULT_CANCELED);
                finish();
            }
        });
    }
    private void setResultOfString() {
        final EditText edittext = (EditText) findViewById(R.id.edittext);
        String s = edittext.getText().toString();
        Intent i = new Intent();
        i.putExtra("org.example.result.s", s);
        setResult(Activity.RESULT_OK, i);
    }
}

TODO

気が向いた時に