jenkins-bot has submitted this change and it was merged.

Change subject: Use newInstance() pattern for SearchFragment
......................................................................


Use newInstance() pattern for SearchFragment

Changes:

• Use newInstance() pattern for SearchFrament. The SearchFragment is no
  longer inflated from XML which has a different lifecycle and is less
  flexible.

• Side effect fix for the Fragment state bug, T145698.

• Remove needless FrameLayout with SearchFragment from fragment_main. No
  other changes intended.

Unintentional changes in behavior and notes:

• The animation to dismiss the search fragment is too quick. An Activity
  (SearchActivity) or Fragment transition should probably be used.

• The query from the previous search is not preserved in the query box
  after dismissing the SearchFragment. This could be added back in but
  seems simpler to keep out for now.

• Searching from m.wikipedia.org is broken but this isn't a regression.

• Tapping a search result from m.wikipedia.org and then tapping the back
  arrow is broken but this isn't a regression.

• The callback structure is weak but this isn't really a regression. A
  future patch should make a SearchActivity and use that.

• There's some similar code in PageActivity and MainFragment that could
  be consolidate with a little work. It seems preferable to make
  SearchActivity in a future patch and just delete this code instead.

• The query is unconditionally trimmed in MainFrament and PageActivity.
  This seems desirable and a future patch may place this trim in
  SearchFragment.newInstance().

Bug: T145698
Bug: T145693
Change-Id: If89136d33827bef75b58795b26641c1628948a3e
---
M app/src/main/java/org/wikipedia/MainFragment.java
M app/src/main/java/org/wikipedia/page/PageActivity.java
M app/src/main/java/org/wikipedia/search/SearchFragment.java
M app/src/main/java/org/wikipedia/search/SearchResultsFragment.java
M app/src/main/res/layout/activity_page.xml
M app/src/main/res/layout/fragment_main.xml
6 files changed, 210 insertions(+), 188 deletions(-)

Approvals:
  Dbrant: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/app/src/main/java/org/wikipedia/MainFragment.java 
b/app/src/main/java/org/wikipedia/MainFragment.java
index d9da094..73e486d 100644
--- a/app/src/main/java/org/wikipedia/MainFragment.java
+++ b/app/src/main/java/org/wikipedia/MainFragment.java
@@ -18,6 +18,7 @@
 import android.view.View;
 import android.view.ViewGroup;
 
+import org.apache.commons.lang3.StringUtils;
 import org.wikipedia.activity.FragmentUtil;
 import org.wikipedia.analytics.GalleryFunnel;
 import org.wikipedia.analytics.IntentFunnel;
@@ -29,8 +30,8 @@
 import org.wikipedia.history.HistoryFragment;
 import org.wikipedia.login.LoginActivity;
 import org.wikipedia.navtab.NavTab;
-import org.wikipedia.nearby.NearbyFragment;
 import org.wikipedia.navtab.NavTabFragmentPagerAdapter;
+import org.wikipedia.nearby.NearbyFragment;
 import org.wikipedia.news.NewsActivity;
 import org.wikipedia.page.ExclusiveBottomSheetPresenter;
 import org.wikipedia.page.PageActivity;
@@ -67,7 +68,6 @@
     @BindView(R.id.fragment_main_view_pager) ViewPager viewPager;
     @BindView(R.id.view_nav_view_pager_tab_layout) TabLayout tabLayout;
     private Unbinder unbinder;
-    private SearchFragment searchFragment;
     private ExclusiveBottomSheetPresenter bottomSheetPresenter;
 
     // The permissions request API doesn't take a callback, so in the event we 
have to
@@ -99,8 +99,6 @@
         tabLayout.setupWithViewPager(viewPager);
 
         bottomSheetPresenter = new 
ExclusiveBottomSheetPresenter(getChildFragmentManager());
-        searchFragment = (SearchFragment) 
getChildFragmentManager().findFragmentById(R.id.search_fragment);
-        searchFragment.setStatusBarVisible(false);
 
         if (savedInstanceState == null) {
             handleIntent(getActivity().getIntent());
@@ -128,7 +126,7 @@
                 && resultCode == Activity.RESULT_OK && data != null
                 && 
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) != null) {
             String searchQuery = 
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS).get(0);
-            openSearchFromIntent(searchQuery, SearchInvokeSource.VOICE);
+            openSearchFragment(SearchInvokeSource.VOICE, searchQuery);
         } else if (requestCode == Constants.ACTIVITY_REQUEST_GALLERY
                 && resultCode == 
GalleryActivity.ACTIVITY_RESULT_FILEPAGE_SELECT) {
             startActivity(data);
@@ -174,17 +172,17 @@
         if (Intent.ACTION_SEND.equals(intent.getAction())
                 && Constants.PLAIN_TEXT_MIME_TYPE.equals(intent.getType())) {
             funnel.logShareIntent();
-            openSearchFromIntent(intent.getStringExtra(Intent.EXTRA_TEXT),
-                    SearchInvokeSource.INTENT_SHARE);
+            openSearchFragment(SearchInvokeSource.INTENT_SHARE,
+                    intent.getStringExtra(Intent.EXTRA_TEXT));
         } else if (Intent.ACTION_PROCESS_TEXT.equals(intent.getAction())
                 && Constants.PLAIN_TEXT_MIME_TYPE.equals(intent.getType())
                 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             funnel.logProcessTextIntent();
-            
openSearchFromIntent(intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT),
-                    SearchInvokeSource.INTENT_PROCESS_TEXT);
+            openSearchFragment(SearchInvokeSource.INTENT_PROCESS_TEXT,
+                    intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT));
         } else if (intent.hasExtra(Constants.INTENT_SEARCH_FROM_WIDGET)) {
             funnel.logSearchWidgetTap();
-            openSearchFromIntent(null, SearchInvokeSource.WIDGET);
+            openSearchFragment(SearchInvokeSource.WIDGET, null);
         } else if (lastPageViewedWithin(1)) {
             startActivity(PageActivity.newIntent(getContext()));
         }
@@ -196,8 +194,7 @@
     }
 
     @Override public void onFeedSearchRequested() {
-        searchFragment.setInvokeSource(SearchInvokeSource.FEED_BAR);
-        searchFragment.openSearch();
+        openSearchFragment(SearchInvokeSource.FEED_BAR, null);
     }
 
     @Override public void onFeedVoiceSearchRequested() {
@@ -323,10 +320,15 @@
     }
 
     @Override
-    public void onSearchClose() {
+    public void onSearchClose(boolean launchedFromIntent) {
+        SearchFragment fragment = searchFragment();
+        if (fragment != null) {
+            closeSearchFragment(fragment);
+        }
+
         Callback callback = callback();
         if (callback != null) {
-            callback.onSearchClose(searchFragment.isLaunchedFromIntent());
+            callback.onSearchClose(launchedFromIntent);
         }
     }
 
@@ -363,12 +365,16 @@
 
     @Override
     public boolean onBackPressed() {
-        Fragment fragment = ((NavTabFragmentPagerAdapter) 
viewPager.getAdapter()).getCurrentFragment();
-        if (searchFragment.onBackPressed()) {
-            return true;
-        } else if (fragment instanceof BackPressedHandler && 
((BackPressedHandler) fragment).onBackPressed()) {
+        SearchFragment searchFragment = searchFragment();
+        if (searchFragment != null && searchFragment.onBackPressed()) {
             return true;
         }
+
+        Fragment fragment = ((NavTabFragmentPagerAdapter) 
viewPager.getAdapter()).getCurrentFragment();
+        if (fragment instanceof BackPressedHandler && ((BackPressedHandler) 
fragment).onBackPressed()) {
+            return true;
+        }
+
         return false;
     }
 
@@ -389,20 +395,6 @@
         FeedbackUtil.showMessage(this, R.string.address_copied);
     }
 
-    public void openSearchFromIntent(@Nullable final String query,
-                                      final SearchInvokeSource source) {
-        tabLayout.post(new Runnable() {
-            @Override
-            public void run() {
-                searchFragment.setInvokeSource(source);
-                searchFragment.openSearch();
-                if (query != null) {
-                    searchFragment.setSearchText(query.trim());
-                }
-            }
-        });
-    }
-
     private boolean lastPageViewedWithin(int days) {
         return TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - 
Prefs.pageLastShown()) < days;
     }
@@ -421,7 +413,26 @@
                 Constants.ACTIVITY_REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION);
     }
 
+    private void openSearchFragment(@NonNull SearchInvokeSource source, 
@Nullable String query) {
+        Fragment fragment = searchFragment();
+        if (fragment == null) {
+            fragment = SearchFragment.newInstance(source, 
StringUtils.trim(query), false);
+            getChildFragmentManager()
+                    .beginTransaction()
+                    .add(R.id.fragment_main_container, fragment)
+                    .commit();
+        }
+    }
+
+    private void closeSearchFragment(@NonNull SearchFragment fragment) {
+        
getChildFragmentManager().beginTransaction().remove(fragment).commitNowAllowingStateLoss();
+    }
+
+    @Nullable private SearchFragment searchFragment() {
+        return (SearchFragment) 
getChildFragmentManager().findFragmentById(R.id.fragment_main_container);
+    }
+
     @Nullable private Callback callback() {
         return FragmentUtil.getCallback(this, Callback.class);
     }
-}
\ No newline at end of file
+}
diff --git a/app/src/main/java/org/wikipedia/page/PageActivity.java 
b/app/src/main/java/org/wikipedia/page/PageActivity.java
index f2ee4a1..5e5b3ba 100644
--- a/app/src/main/java/org/wikipedia/page/PageActivity.java
+++ b/app/src/main/java/org/wikipedia/page/PageActivity.java
@@ -39,6 +39,7 @@
 
 import net.hockeyapp.android.metrics.MetricsManager;
 
+import org.apache.commons.lang3.StringUtils;
 import org.wikipedia.Constants;
 import org.wikipedia.R;
 import org.wikipedia.Site;
@@ -104,7 +105,6 @@
     private Unbinder unbinder;
 
     private PageFragment pageFragment;
-    private SearchFragment searchFragment;
 
     private WikipediaApp app;
     private Bus bus;
@@ -150,7 +150,6 @@
         updateProgressBar(false, true, 0);
 
         pageFragment = (PageFragment) 
getSupportFragmentManager().findFragmentById(R.id.page_fragment);
-        searchFragment = (SearchFragment) 
getSupportFragmentManager().findFragmentById(R.id.page_search_fragment);
         bottomSheetPresenter = new 
ExclusiveBottomSheetPresenter(this.getSupportFragmentManager());
 
         setSupportActionBar(toolbar);
@@ -165,7 +164,7 @@
             isZeroEnabled = 
savedInstanceState.getBoolean("pausedZeroEnabledState");
             currentZeroConfig = 
savedInstanceState.getParcelable("pausedZeroConfig");
             if (savedInstanceState.getBoolean("isSearching")) {
-                searchFragment.openSearch();
+                openSearchFragment(SearchInvokeSource.TOOLBAR, null);
             }
             String language = 
savedInstanceState.getString(LANGUAGE_CODE_BUNDLE_KEY);
             languageChanged = 
!app.getAppOrSystemLanguageCode().equals(language);
@@ -228,7 +227,7 @@
     @Override
     public boolean onSearchRequested() {
         showToolbar();
-        searchFragment.openSearch();
+        openSearchFragment(SearchInvokeSource.TOOLBAR, null);
         return true;
     }
 
@@ -321,6 +320,7 @@
      * @return True if currently searching, false otherwise.
      */
     public boolean isSearching() {
+        SearchFragment searchFragment = searchFragment();
         return searchFragment != null && searchFragment.isSearchActive();
     }
 
@@ -452,12 +452,15 @@
             finishActionMode();
             return;
         }
-        if (searchFragment.onBackPressed()) {
+
+        SearchFragment searchFragment = searchFragment();
+        if (searchFragment != null && searchFragment.onBackPressed()) {
             if (searchFragment.isLaunchedFromIntent()) {
                 finish();
             }
             return;
         }
+
         app.getSessionFunnel().backPressed();
         if (pageFragment.onBackPressed()) {
             return;
@@ -584,8 +587,7 @@
 
     @Override
     public void onPageSearchRequested() {
-        searchFragment.setInvokeSource(SearchInvokeSource.TOOLBAR);
-        searchFragment.openSearch();
+        openSearchFragment(SearchInvokeSource.TOOLBAR, null);
     }
 
     @Override
@@ -600,7 +602,11 @@
     }
 
     @Override
-    public void onSearchClose() {
+    public void onSearchClose(boolean launchedFromIntent) {
+        SearchFragment fragment = searchFragment();
+        if (fragment != null) {
+            closeSearchFragment(fragment);
+        }
         toolbarContainerView.setVisibility(View.VISIBLE);
         hideSoftKeyboard();
     }
@@ -944,4 +950,24 @@
     public void setPageLoadCallbacks(@Nullable PageLoadCallbacks 
pageLoadCallbacks) {
         this.pageLoadCallbacks = pageLoadCallbacks;
     }
+
+    private void openSearchFragment(@NonNull SearchInvokeSource source, 
@Nullable String query) {
+        Fragment fragment = searchFragment();
+        if (fragment == null) {
+            fragment = SearchFragment.newInstance(source, 
StringUtils.trim(query), true);
+            getSupportFragmentManager()
+                    .beginTransaction()
+                    .add(R.id.activity_page_container, fragment)
+                    .commit();
+        }
+    }
+
+    private void closeSearchFragment(@NonNull SearchFragment fragment) {
+        
getSupportFragmentManager().beginTransaction().remove(fragment).commitNowAllowingStateLoss();
+    }
+
+    @Nullable private SearchFragment searchFragment() {
+        return (SearchFragment) getSupportFragmentManager()
+                .findFragmentById(R.id.activity_page_container);
+    }
 }
diff --git a/app/src/main/java/org/wikipedia/search/SearchFragment.java 
b/app/src/main/java/org/wikipedia/search/SearchFragment.java
index 818d771..383fc59 100644
--- a/app/src/main/java/org/wikipedia/search/SearchFragment.java
+++ b/app/src/main/java/org/wikipedia/search/SearchFragment.java
@@ -34,7 +34,9 @@
 import org.wikipedia.history.HistoryEntry;
 import org.wikipedia.page.PageTitle;
 import org.wikipedia.settings.LanguagePreferenceDialog;
+import org.wikipedia.util.DeviceUtil;
 import org.wikipedia.util.FeedbackUtil;
+import org.wikipedia.util.StringUtil;
 import org.wikipedia.views.ViewUtil;
 
 import butterknife.BindView;
@@ -47,12 +49,12 @@
     public interface Callback {
         void onSearchSelectPage(@NonNull HistoryEntry entry, boolean inNewTab);
         void onSearchOpen();
-        void onSearchClose();
+        void onSearchClose(boolean launchedFromIntent);
     }
 
-    private static final String ARG_LAST_SEARCHED_TEXT = "lastSearchedText";
-    private static final String ARG_SEARCH_CURRENT_PANEL = 
"searchCurrentPanel";
     private static final String ARG_INVOKE_SOURCE = "invokeSource";
+    private static final String ARG_QUERY = "lastQuery";
+    private static final String ARG_STATUS_BAR_VISIBLE = "statusBarVisible";
 
     private static final int PANEL_RECENT_SEARCHES = 0;
     private static final int PANEL_SEARCH_RESULTS = 1;
@@ -69,7 +71,7 @@
     private WikipediaApp app;
     @BindView(android.support.v7.appcompat.R.id.search_src_text) EditText 
searchEditText;
     private SearchFunnel funnel;
-    private SearchInvokeSource invokeSource = SearchInvokeSource.TOOLBAR;
+    private SearchInvokeSource invokeSource;
 
     /**
      * Whether the Search fragment is currently showing.
@@ -80,7 +82,8 @@
      * The last search term that the user entered. This will be passed into
      * the TitleSearch and FullSearch sub-fragments.
      */
-    private String lastSearchedText;
+    @Nullable private String query;
+    private boolean statusBarVisible;
 
     private RecentSearchesFragment recentSearchesFragment;
     private SearchResultsFragment searchResultsFragment;
@@ -115,11 +118,30 @@
         }
     };
 
+    @NonNull public static SearchFragment newInstance(@NonNull 
SearchInvokeSource source,
+                                                      @Nullable String query,
+                                                      boolean 
statusBarVisible) {
+        SearchFragment fragment = new SearchFragment();
+
+        Bundle args = new Bundle();
+        args.putInt(ARG_INVOKE_SOURCE, source.code());
+        args.putString(ARG_QUERY, query);
+        args.putBoolean(ARG_STATUS_BAR_VISIBLE, statusBarVisible);
+
+        fragment.setArguments(args);
+        return fragment;
+    }
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         app = WikipediaApp.getInstance();
-        funnel = new SearchFunnel(app, 
SearchInvokeSource.of(invokeSource.code()));
+
+        invokeSource = 
SearchInvokeSource.of(getArguments().getInt(ARG_INVOKE_SOURCE));
+        query = getArguments().getString(ARG_QUERY);
+        statusBarVisible = getArguments().getBoolean(ARG_STATUS_BAR_VISIBLE);
+
+        funnel = new SearchFunnel(app, invokeSource);
     }
 
     @Override
@@ -134,17 +156,13 @@
         View view = inflater.inflate(R.layout.fragment_search, container, 
false);
         unbinder = ButterKnife.bind(this, view);
 
+        statusBarView.setVisibility(statusBarVisible ? View.VISIBLE : 
View.GONE);
+
         FragmentManager childFragmentManager = getChildFragmentManager();
         recentSearchesFragment = 
(RecentSearchesFragment)childFragmentManager.findFragmentById(
                 R.id.search_panel_recent);
         searchResultsFragment = 
(SearchResultsFragment)childFragmentManager.findFragmentById(
                 R.id.fragment_search_results);
-
-        if (savedInstanceState != null) {
-            lastSearchedText = 
savedInstanceState.getString(ARG_LAST_SEARCHED_TEXT);
-            invokeSource = 
SearchInvokeSource.of(savedInstanceState.getInt(ARG_INVOKE_SOURCE));
-            showPanel(savedInstanceState.getInt(ARG_SEARCH_CURRENT_PANEL));
-        }
 
         toolbar.setNavigationOnClickListener(new View.OnClickListener() {
             @Override
@@ -155,6 +173,13 @@
 
         initSearchView();
         initLangButton();
+
+        if (!TextUtils.isEmpty(query)) {
+            showPanel(PANEL_SEARCH_RESULTS);
+        }
+
+        startSearch(query, false);
+
         return view;
     }
 
@@ -168,25 +193,9 @@
     }
 
     @Override
-    public void onSaveInstanceState(Bundle outState) {
-        super.onSaveInstanceState(outState);
-        outState.putString(ARG_LAST_SEARCHED_TEXT, lastSearchedText);
-        outState.putInt(ARG_SEARCH_CURRENT_PANEL, getActivePanel());
-        outState.putInt(ARG_INVOKE_SOURCE, invokeSource.code());
-    }
-
-    @Override
     @NonNull
     public SearchFunnel getFunnel() {
         return funnel;
-    }
-
-    public void setStatusBarVisible(boolean visible) {
-        statusBarView.setVisibility(visible ? View.VISIBLE : View.GONE);
-    }
-
-    public void setInvokeSource(SearchInvokeSource source) {
-        invokeSource = source;
     }
 
     public boolean isLaunchedFromIntent() {
@@ -209,78 +218,6 @@
     }
 
     /**
-     * Kick off a search, based on a given search term. Will automatically 
pass the search to
-     * Title search or Full search, based on which one is currently displayed.
-     * If the search term is empty, the "recent searches" view will be shown.
-     * @param term Phrase to search for.
-     * @param force Whether to "force" starting this search. If the search is 
not forced, the
-     *              search may be delayed by a small time, so that network 
requests are not sent
-     *              too often.  If the search is forced, the network request 
is sent immediately.
-     */
-    public void startSearch(String term, boolean force) {
-        if (TextUtils.isEmpty(term) && !force) {
-            return;
-        }
-        if (!isSearchActive) {
-            openSearch();
-        }
-
-        if (TextUtils.isEmpty(term)) {
-            showPanel(PANEL_RECENT_SEARCHES);
-        } else if (getActivePanel() == PANEL_RECENT_SEARCHES) {
-            //start with title search...
-            showPanel(PANEL_SEARCH_RESULTS);
-        }
-
-        lastSearchedText = term;
-
-        searchResultsFragment.startSearch(term, force);
-    }
-
-    /**
-     * Activate the Search fragment.
-     */
-    public void openSearch() {
-        // create a new funnel every time Search is opened, to get a new 
session ID
-        funnel = new SearchFunnel(WikipediaApp.getInstance(), 
SearchInvokeSource.of(invokeSource.code()));
-        funnel.searchStart();
-        isSearchActive = true;
-        Callback callback = callback();
-        if (callback != null) {
-            callback.onSearchOpen();
-        }
-        // show ourselves
-        ViewUtil.fadeIn(searchContainer);
-
-        // if the current search string is empty, then it's a fresh start, so 
we'll show
-        // recent searches by default. Otherwise, the currently-selected panel 
should already
-        // be visible, so we don't need to do anything.
-        if (TextUtils.isEmpty(lastSearchedText)) {
-            showPanel(PANEL_RECENT_SEARCHES);
-        }
-
-        searchView.setIconified(false);
-        searchView.requestFocusFromTouch();
-        // if we already have a previous search query, then put it into the 
SearchView, and it will
-        // automatically trigger the showing of the corresponding search 
results.
-        if (isValidQuery(lastSearchedText)) {
-            searchView.setQuery(lastSearchedText, false);
-            searchEditText.selectAll();
-        }
-    }
-
-    public void closeSearch() {
-        isSearchActive = false;
-        Callback callback = callback();
-        if (callback != null) {
-            callback.onSearchClose();
-        }
-        // hide ourselves
-        ViewUtil.fadeOut(searchContainer);
-        addRecentSearch(lastSearchedText);
-    }
-
-    /**
      * Determine whether the Search fragment is currently active.
      * @return Whether the Search fragment is active.
      */
@@ -291,6 +228,7 @@
     @Override
     public boolean onBackPressed() {
         if (isSearchActive) {
+            // todo: activity or fragment transition
             closeSearch();
             funnel.searchCancel();
             return true;
@@ -310,11 +248,11 @@
         }
         funnel.searchClick(position);
         HistoryEntry historyEntry = new HistoryEntry(title, 
HistoryEntry.SOURCE_SEARCH);
-        closeSearch();
         Callback callback = callback();
         if (callback != null) {
             callback.onSearchSelectPage(historyEntry, inNewTab);
         }
+        closeSearch();
     }
 
     @Subscribe public void 
onWikipediaZeroStateChangeEvent(WikipediaZeroStateChangeEvent event) {
@@ -329,6 +267,73 @@
 
     @OnClick(R.id.search_lang_button_container) void onLangButtonClick() {
         showLangPreferenceDialog();
+    }
+
+    /**
+     * Kick off a search, based on a given search term. Will automatically 
pass the search to
+     * Title search or Full search, based on which one is currently displayed.
+     * If the search term is empty, the "recent searches" view will be shown.
+     * @param term Phrase to search for.
+     * @param force Whether to "force" starting this search. If the search is 
not forced, the
+     *              search may be delayed by a small time, so that network 
requests are not sent
+     *              too often.  If the search is forced, the network request 
is sent immediately.
+     */
+    private void startSearch(@Nullable String term, boolean force) {
+        if (!isSearchActive) {
+            openSearch();
+        }
+
+        if (TextUtils.isEmpty(term)) {
+            showPanel(PANEL_RECENT_SEARCHES);
+        } else if (getActivePanel() == PANEL_RECENT_SEARCHES) {
+            //start with title search...
+            showPanel(PANEL_SEARCH_RESULTS);
+        }
+
+        query = term;
+
+        if (StringUtil.isBlank(term) && !force) {
+            return;
+        }
+
+        searchResultsFragment.startSearch(term, force);
+    }
+
+    /**
+     * Activate the Search fragment.
+     */
+    private void openSearch() {
+        // create a new funnel every time Search is opened, to get a new 
session ID
+        funnel = new SearchFunnel(WikipediaApp.getInstance(), invokeSource);
+        funnel.searchStart();
+        isSearchActive = true;
+        Callback callback = callback();
+        if (callback != null) {
+            callback.onSearchOpen();
+        }
+        // show ourselves
+        ViewUtil.fadeIn(searchContainer);
+
+        searchView.setIconified(false);
+        searchView.requestFocusFromTouch();
+        // if we already have a previous search query, then put it into the 
SearchView, and it will
+        // automatically trigger the showing of the corresponding search 
results.
+        if (isValidQuery(query)) {
+            searchView.setQuery(query, false);
+            searchEditText.selectAll();
+        }
+    }
+
+    private void closeSearch() {
+        isSearchActive = false;
+        // hide ourselves
+        ViewUtil.fadeOut(searchContainer);
+        DeviceUtil.hideSoftKeyboard(getView());
+        Callback callback = callback();
+        if (callback != null) {
+            callback.onSearchClose(invokeSource.fromIntent());
+        }
+        addRecentSearch(query);
     }
 
     /**
@@ -469,8 +474,8 @@
             public void onDismiss(DialogInterface dialog) {
                 
langButton.setText(app.getAppOrSystemLanguageCode().toUpperCase());
                 formatLangButtonText();
-                if (!TextUtils.isEmpty(lastSearchedText)) {
-                    startSearch(lastSearchedText, true);
+                if (!TextUtils.isEmpty(query)) {
+                    startSearch(query, true);
                 }
             }
         });
diff --git a/app/src/main/java/org/wikipedia/search/SearchResultsFragment.java 
b/app/src/main/java/org/wikipedia/search/SearchResultsFragment.java
index bd8fc71..48f8ab1 100644
--- a/app/src/main/java/org/wikipedia/search/SearchResultsFragment.java
+++ b/app/src/main/java/org/wikipedia/search/SearchResultsFragment.java
@@ -10,6 +10,7 @@
 import org.wikipedia.WikipediaApp;
 import org.wikipedia.readinglist.AddToReadingListDialog;
 import org.wikipedia.util.FeedbackUtil;
+import org.wikipedia.util.StringUtil;
 import org.wikipedia.views.GoneIfEmptyTextView;
 import org.wikipedia.views.ViewUtil;
 import org.wikipedia.views.WikiErrorView;
@@ -175,7 +176,7 @@
      *              search may be delayed by a small time, so that network 
requests are not sent
      *              too often.  If the search is forced, the network request 
is sent immediately.
      */
-    public void startSearch(String term, boolean force) {
+    public void startSearch(@Nullable String term, boolean force) {
         if (!force && currentSearchTerm.equals(term)) {
             return;
         }
@@ -183,7 +184,7 @@
         cancelSearchTask();
         currentSearchTerm = term;
 
-        if (term.isEmpty()) {
+        if (StringUtil.isBlank(term)) {
             clearResults();
             return;
         }
diff --git a/app/src/main/res/layout/activity_page.xml 
b/app/src/main/res/layout/activity_page.xml
index 208735e..d4db551 100644
--- a/app/src/main/res/layout/activity_page.xml
+++ b/app/src/main/res/layout/activity_page.xml
@@ -2,7 +2,7 @@
 <FrameLayout
     xmlns:android="http://schemas.android.com/apk/res/android";
     xmlns:tools="http://schemas.android.com/tools";
-    xmlns:app="http://schemas.android.com/apk/res-auto";
+    android:id="@+id/activity_page_container"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@color/darkest_gray">
@@ -67,13 +67,5 @@
         </FrameLayout>
 
     </LinearLayout>
-
-    <fragment
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:id="@+id/page_search_fragment"
-        android:name="org.wikipedia.search.SearchFragment"
-        android:layout="@layout/fragment_search"
-        tools:layout="@layout/fragment_search" />
 
 </FrameLayout>
diff --git a/app/src/main/res/layout/fragment_main.xml 
b/app/src/main/res/layout/fragment_main.xml
index 97d0864..33fb11e 100644
--- a/app/src/main/res/layout/fragment_main.xml
+++ b/app/src/main/res/layout/fragment_main.xml
@@ -1,44 +1,31 @@
 <?xml version="1.0" encoding="utf-8"?>
-<FrameLayout
+<RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android";
-    xmlns:tools="http://schemas.android.com/tools";
     xmlns:app="http://schemas.android.com/apk/res-auto";
+    android:id="@+id/fragment_main_container"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 
-    <RelativeLayout
+    <org.wikipedia.navtab.NavTabLayout
+        android:id="@+id/view_nav_view_pager_tab_layout"
         android:layout_width="match_parent"
-        android:layout_height="match_parent">
-        <org.wikipedia.navtab.NavTabLayout
-            android:id="@+id/view_nav_view_pager_tab_layout"
-            android:layout_width="match_parent"
-            
android:layout_height="@dimen/view_nav_view_pager_tab_layout_height"
-            android:layout_alignParentBottom="true"
-            app:tabGravity="fill"
-            android:elevation="12dp"
-            android:background="@android:color/white"
-            app:tabIndicatorColor="@android:color/transparent" />
+        android:layout_height="@dimen/view_nav_view_pager_tab_layout_height"
+        android:layout_alignParentBottom="true"
+        app:tabGravity="fill"
+        android:elevation="12dp"
+        android:background="@android:color/white"
+        app:tabIndicatorColor="@android:color/transparent" />
 
-        <android.support.design.widget.CoordinatorLayout
-            android:id="@+id/fragment_main_view_pager_container"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:layout_marginTop="?attr/actionBarSize"
-            android:layout_above="@id/view_nav_view_pager_tab_layout">
-            <org.wikipedia.readinglist.NoSwipeViewPager
-                android:id="@+id/fragment_main_view_pager"
-                android:layout_width="match_parent"
-                android:layout_height="match_parent" />
-        </android.support.design.widget.CoordinatorLayout>
-
-    </RelativeLayout>
-
-    <fragment
+    <android.support.design.widget.CoordinatorLayout
+        android:id="@+id/fragment_main_view_pager_container"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
-        android:id="@+id/search_fragment"
-        android:name="org.wikipedia.search.SearchFragment"
-        android:layout="@layout/fragment_search"
-        tools:layout="@layout/fragment_search" />
+        android:layout_marginTop="?attr/actionBarSize"
+        android:layout_above="@id/view_nav_view_pager_tab_layout">
+        <org.wikipedia.readinglist.NoSwipeViewPager
+            android:id="@+id/fragment_main_view_pager"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent" />
+    </android.support.design.widget.CoordinatorLayout>
 
-</FrameLayout>
+</RelativeLayout>
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/311069
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If89136d33827bef75b58795b26641c1628948a3e
Gerrit-PatchSet: 8
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org>
Gerrit-Reviewer: BearND <bsitzm...@wikimedia.org>
Gerrit-Reviewer: Dbrant <dbr...@wikimedia.org>
Gerrit-Reviewer: Mholloway <mhollo...@wikimedia.org>
Gerrit-Reviewer: Niedzielski <sniedziel...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to