Fix NPE in DoubleTransitionBugFragment
Adjusting the View#findViewById method and FragmentManager#commit method calls to the right timing
Bug: 293262439
Test: gradlew fragment:integration-tests:testapp:connectedAndroidTest
Change-Id: Id28735951ae6ceb6b5b6b5936252684e5b0c5f4c
diff --git a/fragment/integration-tests/testapp/src/androidTest/java/androidx/fragment/testapp/DoubleTransitionBugFragmentTest.kt b/fragment/integration-tests/testapp/src/androidTest/java/androidx/fragment/testapp/DoubleTransitionBugFragmentTest.kt
new file mode 100644
index 0000000..9c93404
--- /dev/null
+++ b/fragment/integration-tests/testapp/src/androidTest/java/androidx/fragment/testapp/DoubleTransitionBugFragmentTest.kt
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.fragment.testapp
+
+import androidx.fragment.app.testing.launchFragment
+import androidx.fragment.testapp.doubleTransitionBug.DoubleTransitionBugFragment
+import androidx.lifecycle.Lifecycle.State
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.filters.LargeTest
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+import org.junit.runner.RunWith
+
+/**
+ * Integration test for {@link DoubleTransitionBugFragment}.
+ */
+@RunWith(AndroidJUnit4::class)
+@LargeTest
+class DoubleTransitionBugFragmentTest {
+ @Test
+ fun launchFragment() {
+ with(launchFragment<DoubleTransitionBugFragment>()) {
+ onFragment { fragment ->
+ assertThat(fragment.lifecycle.currentState).isEqualTo(State.RESUMED)
+ }
+ }
+ }
+}
diff --git a/fragment/integration-tests/testapp/src/main/java/androidx/fragment/testapp/doubleTransitionBug/DoubleTransitionBugFragment.kt b/fragment/integration-tests/testapp/src/main/java/androidx/fragment/testapp/doubleTransitionBug/DoubleTransitionBugFragment.kt
index a239452..9bbb389 100644
--- a/fragment/integration-tests/testapp/src/main/java/androidx/fragment/testapp/doubleTransitionBug/DoubleTransitionBugFragment.kt
+++ b/fragment/integration-tests/testapp/src/main/java/androidx/fragment/testapp/doubleTransitionBug/DoubleTransitionBugFragment.kt
@@ -18,6 +18,7 @@
import android.os.Bundle
import android.view.Gravity
+import android.view.View
import android.widget.Button
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
@@ -35,8 +36,12 @@
add(R.id.content, Fragment(R.layout.double_transition_bug_fragment_second))
}
}
+ }
- requireActivity().findViewById<Button>(R.id.important_button).setOnClickListener {
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+ super.onViewCreated(view, savedInstanceState)
+
+ view.findViewById<Button>(R.id.important_button).setOnClickListener {
switchFragment()
}
}