diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2019-10-15 16:14:48 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-10-15 16:14:48 +0000 |
commit | a4b1e1cd5024dc65e41990ec1438244b3f0fa3ba (patch) | |
tree | c019d559c55cff17307132b300bbfdf985f6b0d6 | |
parent | 819ff2e5e6aea4159abc5241b5ac284fbc04c4c0 (diff) | |
parent | b7f4e647db981a59cfdb394b77fa036c230288c3 (diff) | |
download | support-a4b1e1cd5024dc65e41990ec1438244b3f0fa3ba.tar.gz |
Merge "Add support for class attribute in FragmentContainerView" into androidx-master-dev
3 files changed, 76 insertions, 2 deletions
diff --git a/fragment/fragment/src/androidTest/java/androidx/fragment/app/FragmentContainerInflatedFragmentTest.kt b/fragment/fragment/src/androidTest/java/androidx/fragment/app/FragmentContainerInflatedFragmentTest.kt index 2b928af8ab4..27b945f7c09 100644 --- a/fragment/fragment/src/androidTest/java/androidx/fragment/app/FragmentContainerInflatedFragmentTest.kt +++ b/fragment/fragment/src/androidTest/java/androidx/fragment/app/FragmentContainerInflatedFragmentTest.kt @@ -48,6 +48,20 @@ class FragmentContainerInflatedFragmentTest { } @Test + fun testContentViewWithInflatedFragmentWithClass() { + // The StrictViewFragment runs the appropriate checks to make sure + // we're moving through the states appropriately + with(ActivityScenario.launch(FragmentTestActivity::class.java)) { + val fm = withActivity { + setContentView(R.layout.inflated_fragment_container_view_with_class) + supportFragmentManager + } + val fragment = fm.findFragmentByTag("fragment1") + assertThat(fragment).isNotNull() + } + } + + @Test fun testGetInflatedFragmentInActivityOnCreate() { with(ActivityScenario.launch(ContainerViewActivity::class.java)) { val foundFragment = withActivity { foundFragment } @@ -116,6 +130,31 @@ class FragmentContainerInflatedFragmentTest { } @Test + fun addInflatedFragmentContainerWithClassToGrandParentChildFragmentManager() { + with(ActivityScenario.launch(SimpleContainerActivity::class.java)) { + val grandParent = InflatedParentFragmentContainerWithClass() + withActivity { + supportFragmentManager.beginTransaction() + .add(R.id.fragmentContainer, grandParent) + .commitNow() + } + + val parent = StrictViewFragment(R.layout.fragment_container_view) + + withActivity { + grandParent.childFragmentManager + .beginTransaction() + .add(R.id.fragment_container_view, parent) + .commitNow() + } + + val grandChild = grandParent.childFragmentManager.findFragmentByTag("fragment1") + + assertThat(grandChild).isNotNull() + } + } + + @Test fun addInflatedAfterRestore() { with(ActivityScenario.launch(SimpleContainerActivity::class.java)) { val parent = InflatedParentFragment() @@ -182,6 +221,9 @@ class ContainerViewActivity : FragmentActivity(R.layout.inflated_fragment_contai class InflatedParentFragment : StrictViewFragment(R.layout.inflated_fragment_container_view) +class InflatedParentFragmentContainerWithClass : StrictViewFragment(R.layout + .inflated_fragment_container_view) + class InflatedFragment() : StrictViewFragment() { var name: String? = null diff --git a/fragment/fragment/src/androidTest/res/layout/inflated_fragment_container_view_with_class.xml b/fragment/fragment/src/androidTest/res/layout/inflated_fragment_container_view_with_class.xml new file mode 100644 index 00000000000..0002351917d --- /dev/null +++ b/fragment/fragment/src/androidTest/res/layout/inflated_fragment_container_view_with_class.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright 2019 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. + --> + +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/inflated_container_parent" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <androidx.fragment.app.FragmentContainerView + class="androidx.fragment.app.InflatedFragment" + android:id="@+id/fragment_container_view" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:tag="fragment1"/> +</FrameLayout> diff --git a/fragment/fragment/src/main/java/androidx/fragment/app/FragmentContainerView.java b/fragment/fragment/src/main/java/androidx/fragment/app/FragmentContainerView.java index b74cf988840..315f1f392c8 100644 --- a/fragment/fragment/src/main/java/androidx/fragment/app/FragmentContainerView.java +++ b/fragment/fragment/src/main/java/androidx/fragment/app/FragmentContainerView.java @@ -133,12 +133,15 @@ public final class FragmentContainerView extends FrameLayout { FragmentContainerView( @NonNull Context context, - @Nullable AttributeSet attrs, + @NonNull AttributeSet attrs, @NonNull FragmentManager fm) { super(context, attrs); + String name = attrs.getClassAttribute(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FragmentContainerView); - String name = a.getString(R.styleable.FragmentContainerView_android_name); + if (name == null) { + name = a.getString(R.styleable.FragmentContainerView_android_name); + } String tag = a.getString(R.styleable.FragmentContainerView_android_tag); a.recycle(); |