Fix crash when activity not attached after search index finishes.
When search indexing completes, check for valid activity before proceeding with further operations. Change-Id: I7c72aa358574f79e2184d60bc9d0241595ea5d3b Fix: 38198223 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -291,6 +291,9 @@ public class SearchFragment extends InstrumentedFragment implements SearchView.O
|
||||
*/
|
||||
@Override
|
||||
public void onIndexingFinished() {
|
||||
if (getActivity() == null) {
|
||||
return;
|
||||
}
|
||||
if (mShowingSavedQuery) {
|
||||
mSavedQueryController.loadSavedQueries();
|
||||
} else {
|
||||
|
@@ -349,4 +349,21 @@ public class SearchFragmentTest {
|
||||
|
||||
verify(fragment.mSavedQueryController).loadSavedQueries();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onIndexingFinished_noActivity_shouldNotCrash() {
|
||||
ActivityController<SearchActivity> activityController =
|
||||
Robolectric.buildActivity(SearchActivity.class);
|
||||
activityController.setup();
|
||||
SearchFragment fragment = (SearchFragment) spy(activityController.get().getFragmentManager()
|
||||
.findFragmentById(R.id.main_content));
|
||||
when(mFeatureFactory.searchFeatureProvider.isIndexingComplete(any(Context.class)))
|
||||
.thenReturn(true);
|
||||
fragment.mQuery = "bright";
|
||||
ReflectionHelpers.setField(fragment, "mLoaderManager", null);
|
||||
ReflectionHelpers.setField(fragment, "mHost", null);
|
||||
|
||||
fragment.onIndexingFinished();
|
||||
// no crash
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user