Add LoaderTest.testRedeliverWhenReattached
Ensure that Loaders will redeliver their data
(i.e., call LoaderCallbacks.onLoadFinished())
after a Fragment is detached and reattached.
This handles the edge case where the Fragment
does not get fully destroyed, but its view
hierarchy is fully destroyed and recreated.
Test: LoaderTest.testRedeliverWhenReattached
BUG: 70897911
Change-Id: Id1384093c013f6e3577a1cce9a36bbb0e9acb940
diff --git a/fragment/tests/java/android/support/v4/app/LoaderTest.java b/fragment/tests/java/android/support/v4/app/LoaderTest.java
index d3bfe44..2e552df 100644
--- a/fragment/tests/java/android/support/v4/app/LoaderTest.java
+++ b/fragment/tests/java/android/support/v4/app/LoaderTest.java
@@ -18,6 +18,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -26,6 +27,7 @@
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
+import android.support.fragment.test.R;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import android.support.test.rule.ActivityTestRule;
@@ -112,6 +114,33 @@
assertEquals("Loaded!", activity.textView.getText().toString());
}
+ @Test
+ public void testRedeliverWhenReattached() throws Throwable {
+ LoaderActivity activity = mActivityRule.getActivity();
+
+ FragmentManager fm = activity.getSupportFragmentManager();
+
+ LoaderActivity.TextLoaderFragment fragment =
+ (LoaderActivity.TextLoaderFragment) fm.findFragmentById(R.id.fragmentContainer);
+
+ assertNotNull(fragment);
+ assertEquals("Loaded!", fragment.textView.getText().toString());
+
+ fm.beginTransaction()
+ .detach(fragment)
+ .commit();
+
+ FragmentTestUtil.executePendingTransactions(mActivityRule, fm);
+
+ fm.beginTransaction()
+ .attach(fragment)
+ .commit();
+
+ FragmentTestUtil.executePendingTransactions(mActivityRule, fm);
+
+ assertEquals("Loaded!", fragment.textView.getText().toString());
+ }
+
@Test(expected = IllegalStateException.class)
public void enforceOnMainThread_initLoader() {
LoaderActivity activity = mActivityRule.getActivity();
diff --git a/fragment/tests/java/android/support/v4/app/test/LoaderActivity.java b/fragment/tests/java/android/support/v4/app/test/LoaderActivity.java
index 812cc97..cb02efd 100644
--- a/fragment/tests/java/android/support/v4/app/test/LoaderActivity.java
+++ b/fragment/tests/java/android/support/v4/app/test/LoaderActivity.java
@@ -22,13 +22,17 @@
import android.support.annotation.Nullable;
import android.support.fragment.test.R;
import android.support.testutils.RecreatedActivity;
+import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
+import android.view.LayoutInflater;
+import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
-public class LoaderActivity extends RecreatedActivity {
+public class LoaderActivity extends RecreatedActivity
+ implements LoaderManager.LoaderCallbacks<String> {
public TextView textView;
public TextView textViewB;
@@ -36,35 +40,37 @@
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.fragment_a);
+ setContentView(R.layout.activity_loader);
textView = findViewById(R.id.textA);
- ViewGroup container = (ViewGroup) textView.getParent();
- textViewB = new TextView(this);
- textViewB.setId(R.id.textB);
- container.addView(textViewB);
+ textViewB = findViewById(R.id.textB);
+
+ if (savedInstanceState == null) {
+ getSupportFragmentManager()
+ .beginTransaction()
+ .add(R.id.fragmentContainer, new TextLoaderFragment())
+ .commit();
+ }
}
@Override
protected void onResume() {
super.onResume();
- getSupportLoaderManager().initLoader(0, null, new TextLoaderCallback());
+ getSupportLoaderManager().initLoader(0, null, this);
}
- class TextLoaderCallback implements LoaderManager.LoaderCallbacks<String> {
- @NonNull
- @Override
- public Loader<String> onCreateLoader(int id, Bundle args) {
- return new TextLoader(LoaderActivity.this);
- }
+ @NonNull
+ @Override
+ public Loader<String> onCreateLoader(int id, @Nullable Bundle args) {
+ return new TextLoader(this);
+ }
- @Override
- public void onLoadFinished(@NonNull Loader<String> loader, String data) {
- textView.setText(data);
- }
+ @Override
+ public void onLoadFinished(@NonNull Loader<String> loader, String data) {
+ textView.setText(data);
+ }
- @Override
- public void onLoaderReset(@NonNull Loader<String> loader) {
- }
+ @Override
+ public void onLoaderReset(@NonNull Loader<String> loader) {
}
static class TextLoader extends AsyncTaskLoader<String> {
@@ -82,4 +88,42 @@
return "Loaded!";
}
}
+
+ public static class TextLoaderFragment extends Fragment
+ implements LoaderManager.LoaderCallbacks<String> {
+ public TextView textView;
+
+ @Override
+ public void onCreate(@Nullable Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ getLoaderManager().initLoader(0, null, this);
+ }
+
+ @Nullable
+ @Override
+ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
+ @Nullable Bundle savedInstanceState) {
+ return inflater.inflate(R.layout.fragment_c, container, false);
+ }
+
+ @Override
+ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
+ textView = view.findViewById(R.id.textC);
+ }
+
+ @NonNull
+ @Override
+ public Loader<String> onCreateLoader(int id, @Nullable Bundle args) {
+ return new TextLoader(getContext());
+ }
+
+ @Override
+ public void onLoadFinished(@NonNull Loader<String> loader, String data) {
+ textView.setText(data);
+ }
+
+ @Override
+ public void onLoaderReset(@NonNull Loader<String> loader) {
+ }
+ }
}
diff --git a/fragment/tests/res/layout/activity_loader.xml b/fragment/tests/res/layout/activity_loader.xml
new file mode 100644
index 0000000..06343a3
--- /dev/null
+++ b/fragment/tests/res/layout/activity_loader.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright 2018 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.
+ -->
+
+<LinearLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical">
+ <TextView
+ android:id="@+id/textA"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/hello"/>
+ <TextView
+ android:id="@+id/textB"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/hello"/>
+ <FrameLayout
+ android:id="@+id/fragmentContainer"
+ android:layout_width="match_parent"
+ android:layout_height="0dp"
+ android:layout_weight="1"/>
+</LinearLayout>