0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Android]画面遷移(備忘録)

0
Last updated at Posted at 2016-06-10

画面遷移はアプリの基本なので備忘録的な意味で書きます。
これが分かるだけで開発の幅も広がると思います。

<結果を遷移元に戻さない>

MainActivity.java(遷移元)
// インテントの作成&遷移先を指定
Intent intent = new Intent(this, ResultActivity.class);
// 遷移先に渡すオブジェクトを設定
intent.putExtra("KEY", value);
// 遷移先に移る
startActivity(intent);
ResultActivity.java(遷移先)
// Intentを取得する
Intent intent = getIntent();
// 渡されたオブジェクトを取得する
value = intent.getDoubleExtra("KEY",0.0);
AndroidManifest.xml
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name="com.example.student.calcbmi.MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
            android:name="com.example.student.calcbmi.ResultActivity"
            android:label="@string/app_name" >
    </activity>
</application>

<結果を遷移元に戻す>

MainActivity.java(遷移元)
// インテントの作成&遷移先を指定
Intent intent = new Intent(this, ResultActivity.class);
// 遷移先に渡すオブジェクトを設定
intent.putExtra("KEY", value);
// 遷移先に移る(遷移先から結果を送り返す)
startActivityForResult(intent, 0);
ResultActivity.java(遷移先)
// Intentを取得する
Intent intent = getIntent();
// 渡されたオブジェクトを取得する
value = intent.getDoubleExtra("KEY",0.0);
// インテントの作成
Intent intent = new Intent();
// 遷移元に送り返すオブジェクトを設定
intent.putExtra("MESSAGE", message);
// 結果コードを設定
setResult(RESULT_OK,intent);
// 遷移先を閉じる
finish();
MainActivity.java(遷移元)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
       // 遷移先からオブジェクトを受け取る
            String message = data.getStringExtra("MESSAGE");
       // トーストを表示する
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        }
    }
}
AndroidManifest.xml
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name="com.example.student.calcbmi.MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
            android:name="com.example.student.calcbmi.ResultActivity"
            android:label="@string/app_name" >
    </activity>
</application>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?