回答編集履歴
1
fragment3 でテキストを入力して ViewModel に保存し fragment1 で表示するようにしてみました
test
CHANGED
@@ -122,6 +122,10 @@
|
|
122
122
|
|
123
123
|
val func1_mode = MutableLiveData<Int>(1)
|
124
124
|
|
125
|
+
|
126
|
+
|
127
|
+
val func1_text = MutableLiveData<String>("dummy")
|
128
|
+
|
125
129
|
}
|
126
130
|
|
127
131
|
```
|
@@ -178,6 +182,12 @@
|
|
178
182
|
|
179
183
|
}
|
180
184
|
|
185
|
+
viewModel.func1_text.observe(viewLifecycleOwner) { text ->
|
186
|
+
|
187
|
+
func1_text.text = text
|
188
|
+
|
189
|
+
}
|
190
|
+
|
181
191
|
}
|
182
192
|
|
183
193
|
}
|
@@ -232,6 +242,10 @@
|
|
232
242
|
|
233
243
|
import androidx.fragment.app.Fragment
|
234
244
|
|
245
|
+
import androidx.fragment.app.activityViewModels
|
246
|
+
|
247
|
+
import kotlinx.android.synthetic.main.fragment3.*
|
248
|
+
|
235
249
|
|
236
250
|
|
237
251
|
class Fragment3 : Fragment() {
|
@@ -248,6 +262,28 @@
|
|
248
262
|
|
249
263
|
}
|
250
264
|
|
265
|
+
|
266
|
+
|
267
|
+
private val viewModel: SampleViewModel by activityViewModels()
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
272
|
+
|
273
|
+
super.onViewCreated(view, savedInstanceState)
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
second_button.setOnClickListener {
|
278
|
+
|
279
|
+
viewModel.func1_text.value = func1_edit.text.toString()
|
280
|
+
|
281
|
+
viewModel.func1_mode.value = 1
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
}
|
286
|
+
|
251
287
|
}
|
252
288
|
|
253
289
|
```
|
@@ -370,6 +406,8 @@
|
|
370
406
|
|
371
407
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
372
408
|
|
409
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
410
|
+
|
373
411
|
xmlns:tools="http://schemas.android.com/tools"
|
374
412
|
|
375
413
|
android:layout_width="match_parent"
|
@@ -378,23 +416,85 @@
|
|
378
416
|
|
379
417
|
tools:context=".Fragment1">
|
380
418
|
|
419
|
+
|
420
|
+
|
381
421
|
<Button
|
382
422
|
|
423
|
+
android:id="@+id/first_button"
|
424
|
+
|
425
|
+
android:layout_width="wrap_content"
|
426
|
+
|
427
|
+
android:layout_height="wrap_content"
|
428
|
+
|
383
429
|
android:text="to Fragment3"
|
384
430
|
|
431
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
432
|
+
|
433
|
+
app:layout_constraintEnd_toEndOf="parent"
|
434
|
+
|
435
|
+
app:layout_constraintStart_toStartOf="parent"
|
436
|
+
|
437
|
+
app:layout_constraintTop_toBottomOf="@id/func1_text" />
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
<TextView
|
442
|
+
|
385
|
-
android:id="@+id/f
|
443
|
+
android:id="@+id/func1_text"
|
444
|
+
|
445
|
+
android:layout_width="0dp"
|
446
|
+
|
447
|
+
android:layout_height="wrap_content"
|
448
|
+
|
449
|
+
android:layout_marginStart="16dp"
|
450
|
+
|
451
|
+
android:layout_marginEnd="16dp"
|
452
|
+
|
453
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
454
|
+
|
455
|
+
app:layout_constraintEnd_toEndOf="parent"
|
456
|
+
|
457
|
+
app:layout_constraintStart_toStartOf="parent"
|
458
|
+
|
459
|
+
app:layout_constraintTop_toTopOf="parent" />
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
464
|
+
|
465
|
+
```
|
466
|
+
|
467
|
+
fragment2.xml
|
468
|
+
|
469
|
+
```xml
|
470
|
+
|
471
|
+
<?xml version="1.0" encoding="utf-8"?>
|
472
|
+
|
473
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
474
|
+
|
475
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
476
|
+
|
477
|
+
xmlns:tools="http://schemas.android.com/tools"
|
478
|
+
|
479
|
+
android:layout_width="match_parent"
|
480
|
+
|
481
|
+
android:layout_height="match_parent"
|
482
|
+
|
483
|
+
tools:context=".Fragment2">
|
484
|
+
|
485
|
+
<TextView
|
386
486
|
|
387
487
|
android:layout_width="match_parent"
|
388
488
|
|
389
489
|
android:layout_height="match_parent"
|
390
490
|
|
391
|
-
/>
|
491
|
+
android:text="Fragment2" />
|
392
492
|
|
393
493
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
394
494
|
|
395
495
|
```
|
396
496
|
|
397
|
-
fragment
|
497
|
+
fragment3.xml
|
398
498
|
|
399
499
|
```xml
|
400
500
|
|
@@ -404,27 +504,79 @@
|
|
404
504
|
|
405
505
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
406
506
|
|
507
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
508
|
+
|
407
509
|
xmlns:tools="http://schemas.android.com/tools"
|
408
510
|
|
409
511
|
android:layout_width="match_parent"
|
410
512
|
|
411
513
|
android:layout_height="match_parent"
|
412
514
|
|
413
|
-
tools:context=".Fragment
|
515
|
+
tools:context=".Fragment3">
|
516
|
+
|
517
|
+
|
518
|
+
|
519
|
+
<EditText
|
520
|
+
|
521
|
+
android:id="@+id/func1_edit"
|
522
|
+
|
523
|
+
android:layout_width="0dp"
|
524
|
+
|
525
|
+
android:layout_height="wrap_content"
|
526
|
+
|
527
|
+
android:layout_marginStart="16dp"
|
528
|
+
|
529
|
+
android:layout_marginEnd="16dp"
|
530
|
+
|
531
|
+
android:ems="10"
|
532
|
+
|
533
|
+
android:text="Fragment3"
|
534
|
+
|
535
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
536
|
+
|
537
|
+
app:layout_constraintEnd_toEndOf="parent"
|
538
|
+
|
539
|
+
app:layout_constraintStart_toStartOf="parent"
|
540
|
+
|
541
|
+
app:layout_constraintTop_toTopOf="parent" />
|
542
|
+
|
543
|
+
<Button
|
544
|
+
|
545
|
+
android:id="@+id/second_button"
|
546
|
+
|
547
|
+
android:layout_width="wrap_content"
|
548
|
+
|
549
|
+
android:layout_height="wrap_content"
|
550
|
+
|
551
|
+
android:text="set Text"
|
552
|
+
|
553
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
554
|
+
|
555
|
+
app:layout_constraintEnd_toEndOf="parent"
|
556
|
+
|
557
|
+
app:layout_constraintStart_toStartOf="parent"
|
558
|
+
|
559
|
+
app:layout_constraintTop_toBottomOf="@id/func1_edit" />
|
414
560
|
|
415
561
|
<TextView
|
416
562
|
|
563
|
+
android:id="@+id/textView"
|
564
|
+
|
417
|
-
android:layout_width="
|
565
|
+
android:layout_width="wrap_content"
|
418
|
-
|
566
|
+
|
419
|
-
android:layout_height="
|
567
|
+
android:layout_height="wrap_content"
|
420
|
-
|
568
|
+
|
421
|
-
android:text="
|
569
|
+
android:text="Input Text:"
|
570
|
+
|
571
|
+
app:layout_constraintStart_toStartOf="@id/func1_edit"
|
572
|
+
|
573
|
+
app:layout_constraintBottom_toTopOf="@id/func1_edit" />
|
422
574
|
|
423
575
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
424
576
|
|
425
577
|
```
|
426
578
|
|
427
|
-
fragment
|
579
|
+
fragment4.xml
|
428
580
|
|
429
581
|
```xml
|
430
582
|
|
@@ -440,40 +592,10 @@
|
|
440
592
|
|
441
593
|
android:layout_height="match_parent"
|
442
594
|
|
595
|
+
android:id="@+id/function1_container"
|
596
|
+
|
443
|
-
tools:context=".Fragment
|
597
|
+
tools:context=".Fragment4">
|
444
|
-
|
445
|
-
<TextView
|
446
|
-
|
447
|
-
android:layout_width="match_parent"
|
448
|
-
|
449
|
-
android:layout_height="match_parent"
|
450
|
-
|
451
|
-
android:text="Fragment3" />
|
452
598
|
|
453
599
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
454
600
|
|
455
601
|
```
|
456
|
-
|
457
|
-
fragment4.xml
|
458
|
-
|
459
|
-
```xml
|
460
|
-
|
461
|
-
<?xml version="1.0" encoding="utf-8"?>
|
462
|
-
|
463
|
-
<androidx.constraintlayout.widget.ConstraintLayout
|
464
|
-
|
465
|
-
xmlns:android="http://schemas.android.com/apk/res/android"
|
466
|
-
|
467
|
-
xmlns:tools="http://schemas.android.com/tools"
|
468
|
-
|
469
|
-
android:layout_width="match_parent"
|
470
|
-
|
471
|
-
android:layout_height="match_parent"
|
472
|
-
|
473
|
-
android:id="@+id/function1_container"
|
474
|
-
|
475
|
-
tools:context=".Fragment4">
|
476
|
-
|
477
|
-
</androidx.constraintlayout.widget.ConstraintLayout>
|
478
|
-
|
479
|
-
```
|