teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

fragment3 でテキストを入力して ViewModel に保存し fragment1 で表示するようにしてみました

2021/07/20 12:17

投稿

jimbe
jimbe

スコア13352

answer CHANGED
@@ -60,6 +60,8 @@
60
60
 
61
61
  class SampleViewModel : ViewModel() {
62
62
  val func1_mode = MutableLiveData<Int>(1)
63
+
64
+ val func1_text = MutableLiveData<String>("dummy")
63
65
  }
64
66
  ```
65
67
  Fragment1.kt
@@ -88,6 +90,9 @@
88
90
  first_button.setOnClickListener {
89
91
  viewModel.func1_mode.value = 3
90
92
  }
93
+ viewModel.func1_text.observe(viewLifecycleOwner) { text ->
94
+ func1_text.text = text
95
+ }
91
96
  }
92
97
  }
93
98
  ```
@@ -115,6 +120,8 @@
115
120
  import android.view.View
116
121
  import android.view.ViewGroup
117
122
  import androidx.fragment.app.Fragment
123
+ import androidx.fragment.app.activityViewModels
124
+ import kotlinx.android.synthetic.main.fragment3.*
118
125
 
119
126
  class Fragment3 : Fragment() {
120
127
  override fun onCreateView(
@@ -123,6 +130,17 @@
123
130
  ): View? {
124
131
  return inflater.inflate(R.layout.fragment3, container, false)
125
132
  }
133
+
134
+ private val viewModel: SampleViewModel by activityViewModels()
135
+
136
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
137
+ super.onViewCreated(view, savedInstanceState)
138
+
139
+ second_button.setOnClickListener {
140
+ viewModel.func1_text.value = func1_edit.text.toString()
141
+ viewModel.func1_mode.value = 1
142
+ }
143
+ }
126
144
  }
127
145
  ```
128
146
  Fragment4.kt
@@ -184,16 +202,33 @@
184
202
  <?xml version="1.0" encoding="utf-8"?>
185
203
  <androidx.constraintlayout.widget.ConstraintLayout
186
204
  xmlns:android="http://schemas.android.com/apk/res/android"
205
+ xmlns:app="http://schemas.android.com/apk/res-auto"
187
206
  xmlns:tools="http://schemas.android.com/tools"
188
207
  android:layout_width="match_parent"
189
208
  android:layout_height="match_parent"
190
209
  tools:context=".Fragment1">
210
+
191
211
  <Button
212
+ android:id="@+id/first_button"
213
+ android:layout_width="wrap_content"
214
+ android:layout_height="wrap_content"
192
215
  android:text="to Fragment3"
216
+ app:layout_constraintBottom_toBottomOf="parent"
217
+ app:layout_constraintEnd_toEndOf="parent"
218
+ app:layout_constraintStart_toStartOf="parent"
219
+ app:layout_constraintTop_toBottomOf="@id/func1_text" />
220
+
221
+ <TextView
193
- android:id="@+id/first_button"
222
+ android:id="@+id/func1_text"
194
- android:layout_width="match_parent"
223
+ android:layout_width="0dp"
195
- android:layout_height="match_parent"
224
+ android:layout_height="wrap_content"
225
+ android:layout_marginStart="16dp"
196
- />
226
+ android:layout_marginEnd="16dp"
227
+ app:layout_constraintBottom_toBottomOf="parent"
228
+ app:layout_constraintEnd_toEndOf="parent"
229
+ app:layout_constraintStart_toStartOf="parent"
230
+ app:layout_constraintTop_toTopOf="parent" />
231
+
197
232
  </androidx.constraintlayout.widget.ConstraintLayout>
198
233
  ```
199
234
  fragment2.xml
@@ -216,14 +251,40 @@
216
251
  <?xml version="1.0" encoding="utf-8"?>
217
252
  <androidx.constraintlayout.widget.ConstraintLayout
218
253
  xmlns:android="http://schemas.android.com/apk/res/android"
254
+ xmlns:app="http://schemas.android.com/apk/res-auto"
219
255
  xmlns:tools="http://schemas.android.com/tools"
220
256
  android:layout_width="match_parent"
221
257
  android:layout_height="match_parent"
222
258
  tools:context=".Fragment3">
259
+
260
+ <EditText
261
+ android:id="@+id/func1_edit"
262
+ android:layout_width="0dp"
263
+ android:layout_height="wrap_content"
264
+ android:layout_marginStart="16dp"
265
+ android:layout_marginEnd="16dp"
266
+ android:ems="10"
267
+ android:text="Fragment3"
268
+ app:layout_constraintBottom_toBottomOf="parent"
269
+ app:layout_constraintEnd_toEndOf="parent"
270
+ app:layout_constraintStart_toStartOf="parent"
271
+ app:layout_constraintTop_toTopOf="parent" />
272
+ <Button
273
+ android:id="@+id/second_button"
274
+ android:layout_width="wrap_content"
275
+ android:layout_height="wrap_content"
276
+ android:text="set Text"
277
+ app:layout_constraintBottom_toBottomOf="parent"
278
+ app:layout_constraintEnd_toEndOf="parent"
279
+ app:layout_constraintStart_toStartOf="parent"
280
+ app:layout_constraintTop_toBottomOf="@id/func1_edit" />
223
281
  <TextView
282
+ android:id="@+id/textView"
224
- android:layout_width="match_parent"
283
+ android:layout_width="wrap_content"
225
- android:layout_height="match_parent"
284
+ android:layout_height="wrap_content"
226
- android:text="Fragment3" />
285
+ android:text="Input Text:"
286
+ app:layout_constraintStart_toStartOf="@id/func1_edit"
287
+ app:layout_constraintBottom_toTopOf="@id/func1_edit" />
227
288
  </androidx.constraintlayout.widget.ConstraintLayout>
228
289
  ```
229
290
  fragment4.xml