0
0

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 1 year has passed since last update.

a-2-1(2).通常の画面遷移(Intent)とデータ受け渡し

Last updated at Posted at 2022-10-11

a-2-1(2).通常の画面遷移(Intent)とデータ受け渡し

目標設定

一覧に戻る

課題

  1. Intentを利用して画面遷移ができるか。
  2. Intentを利用して画面遷移後に元の画面に戻ることができるか。
  3. Intentを利用して画面遷移時にパラメーターを渡すことができるか。

Github

テスト実装

IntentTestActivity.kt
class IntentTestActivity : AppCompatActivity(), View.OnClickListener {
    companion object {
        const val CHILDREN_STATES = "children_states"
    }

    private lateinit var moveButton: Button
    private var childrenStates: Boolean = false

    fun createIntent(context: Context, childrenStates: Boolean): Intent {
        // 3. Intentを利用して画面遷移時にパラメーターを渡すことができるか。
        // ・可能でした。
        val intent = Intent(
            context,
            IntentTestActivity::class.java
        ).apply {
            putExtra(CHILDREN_STATES, childrenStates)
        }

        return intent
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_intent_test)

        childrenStates = intent.getBooleanExtra(
            CHILDREN_STATES, false
        )

        moveButton = findViewById<Button>(R.id.moveButton).apply {
            if (!childrenStates) {
                text = "画面遷移"
            } else {
                text = "戻る"
            }
        }

        moveButton.setOnClickListener(this)
    }

    override fun onClick(view: View){
        when (view.id) {
            R.id.moveButton -> {
                if (!childrenStates) {
                    // 1. Intentを利用して画面遷移ができるか。
                    // ・可能でした。
                    startActivity(createIntent(this, true))
                } else {
                    // 2. Intentを利用して画面遷移後に元の画面に戻ることができるか。
                    // ・可能でした。
                    finish()
                }
            }
        }
    }
}
activity_intent_test.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginStart="23dp"
        android:layout_marginEnd="23dp"
        android:layout_marginTop="23dp"
        android:layout_marginBottom="23dp"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:orientation="horizontal"
            tools:ignore="UselessParent">

            <Button
                android:id="@+id/moveButton"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="画面遷移"
                tools:ignore="HardcodedText" />

            <Space
                android:layout_width="30dp"
                android:layout_height="match_parent" />

        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?