Androidでのページ遷移とデータの引き継ぎ
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dentaku.com.sendaoi"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".dentaku"
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=".subform" android:label="@string/app_name"></activity>
</application>
</manifest>
</pre>
呼び元
// Button01 onClickイベント
private void Button01_onClick() {
// インテントへのインスタンス生成
Intent intent = new Intent(Main.this,Sub_form.class);
// メイン画面 EditText01 テキストを取得
CharSequence text = edittext.getText();
// サブ画面(インテント)にメイン画面 EditText01 テキストを送信
intent.putExtra("MAIN_FORM_TEXT", text);
// サブ画面(インテント)の起動
startActivityForResult(intent,SHOW_SUB_FORM);
}
// メイン画面から呼び出した画面から戻る際に呼び出しされるメソッド
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data){
if (requestCode == SHOW_SUB_FORM) {
if (resultCode == RESULT_OK){
edittext.setText(data.getCharSequenceExtra("SUB_FORM_TEXT"));
}
}
}
呼び先
public void onCreate(Bundle savedInstanceState) {
// ~ 省略 ~
// メイン画面から渡されたインテントオブジェクトを取得
Bundle extras = getIntent().getExtras();
if (extras != null){
// サブ画面 TextView01 へのインスタンス生成
TextView textView = (TextView) findViewById(R.id.TextView02);
// メイン画面から送信された EditText01 に入力された文字列をサブ画面 TextView02 に設定
textView.setText(extras.getCharSequence("MAIN_FORM_TEXT"));
}
}
// Button02 onClickイベント
private void Button02_onClick(){
// インテントのインスタンスを生成
Intent intent = new Intent();
// メイン画面に送る文字列設定
CharSequence text = edittext.getText();
// メイン画面(インテント)に text に設定したテキストを送信
intent.putExtra("SUB_FORM_TEXT", text);
// メイン画面(インテント)に返却コードを送信
setResult(RESULT_OK, intent);
// サブ画面の終了
finish();
}