Skip to content

Instantly share code, notes, and snippets.

@Nunocky
Created June 25, 2023 00:57
Show Gist options
  • Save Nunocky/18d548ce197592e638b890ec14ccc2e8 to your computer and use it in GitHub Desktop.
Save Nunocky/18d548ce197592e638b890ec14ccc2e8 to your computer and use it in GitHub Desktop.
Fragmentの値の保存と復元
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainFragment">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button1"
app:layout_constraintTop_toTopOf="@+id/button1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.annotation.VisibleForTesting
class MainFragment : Fragment() {
@VisibleForTesting
var value1: Int = DEFAULT_VALUE1
@VisibleForTesting
var value2: String = DEFAULT_VALUE2
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("value1", value1)
outState.putString("value2", value2)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
value1 = savedInstanceState?.getInt("value1") ?: DEFAULT_VALUE1
value2 = savedInstanceState?.getString("value2") ?: DEFAULT_VALUE2
view.findViewById<Button>(R.id.button1).setOnClickListener {
value1 = 0
value2 = "foo"
Log.d("MainFragment", "button1 clicked")
updateViews()
}
view.findViewById<Button>(R.id.button2).setOnClickListener {
value1 = 1
value2 = "bar"
Log.d("MainFragment", "button2 clicked")
updateViews()
}
}
override fun onResume() {
super.onResume()
Log.d("MainFragment", "onResume")
updateViews()
}
private fun updateViews() {
Log.d("MainFragment", "updateViews: $value1, $value2")
view?.findViewById<TextView>(R.id.textView1)?.text = "$value1"
view?.findViewById<TextView>(R.id.textView2)?.text = value2
}
companion object {
const val DEFAULT_VALUE1 = 0
const val DEFAULT_VALUE2 = "foo"
}
}
package com.example.myapplication
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class MainFragmentTest {
@Test
fun `初期状態のテキスト表示`() {
val scenario = launchFragmentInContainer<MainFragment>()
scenario.onFragment() {
assertEquals(0, it.value1)
assertEquals("foo", it.value2)
}
}
@Test
fun `ボタン2をクリックしたときのテキスト表示`() {
val scenario = launchFragmentInContainer<MainFragment>()
onView(withId(R.id.button2)).perform(click())
scenario.onFragment() {
assertEquals(1, it.value1)
assertEquals("bar", it.value2)
}
}
@Test
fun `ボタン2をクリックしたあとでフラグメントが再生成`() {
val scenario = launchFragmentInContainer<MainFragment>()
onView(withId(R.id.button2)).perform(click())
scenario.recreate()
scenario.onFragment() {
assertEquals(1, it.value1)
assertEquals("bar", it.value2)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment