Merge pull request #17 from ericedens/fix_activity_lifecycle
Bug 1033686 - Pre-search and post-search are visible at the same time
This commit is contained in:
Коммит
630eff9669
|
@ -0,0 +1,102 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.search.test;
|
||||
|
||||
import android.test.ActivityInstrumentationTestCase2;
|
||||
|
||||
import org.mozilla.search.Constants;
|
||||
import org.mozilla.search.MainActivity;
|
||||
import org.mozilla.search.PostSearchFragment;
|
||||
import org.mozilla.search.autocomplete.SearchFragment;
|
||||
import org.mozilla.search.PreSearchFragment;
|
||||
|
||||
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
|
||||
|
||||
public MainActivityTest() {
|
||||
super(MainActivity.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public void testPreConditions() {
|
||||
assertPreSearchVisible();
|
||||
}
|
||||
|
||||
public void testSearch() {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getActivity().onSearch("cable");
|
||||
getActivity().getSupportFragmentManager().executePendingTransactions();
|
||||
}
|
||||
});
|
||||
getInstrumentation().waitForIdleSync();
|
||||
|
||||
assertPostSearchVisible();
|
||||
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getActivity().onBackPressed();
|
||||
}
|
||||
});
|
||||
getInstrumentation().waitForIdleSync();
|
||||
|
||||
assertPreSearchVisible();
|
||||
}
|
||||
|
||||
public void testDestroy() {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getActivity().onSearch("cable");
|
||||
getActivity().getSupportFragmentManager().executePendingTransactions();
|
||||
}
|
||||
});
|
||||
getInstrumentation().waitForIdleSync();
|
||||
assertPostSearchVisible();
|
||||
|
||||
getActivity().finish();
|
||||
getInstrumentation().waitForIdleSync();
|
||||
setActivity(null);
|
||||
|
||||
// Restart the activity.
|
||||
getActivity();
|
||||
|
||||
getInstrumentation().waitForIdleSync();
|
||||
|
||||
assertPreSearchVisible();
|
||||
}
|
||||
|
||||
private PostSearchFragment getPostSearchFragment() {
|
||||
return (PostSearchFragment) getActivity()
|
||||
.getSupportFragmentManager().findFragmentByTag(Constants.POSTSEARCH_FRAGMENT);
|
||||
}
|
||||
|
||||
private PreSearchFragment getPreSearchFragment() {
|
||||
return (PreSearchFragment) getActivity()
|
||||
.getSupportFragmentManager().findFragmentByTag(Constants.PRESEARCH_FRAGMENT);
|
||||
}
|
||||
|
||||
private SearchFragment getSearchFragment() {
|
||||
return (SearchFragment) getActivity()
|
||||
.getSupportFragmentManager().findFragmentByTag(Constants.SEARCH_FRAGMENT);
|
||||
}
|
||||
|
||||
private void assertPreSearchVisible() {
|
||||
assertTrue(getPostSearchFragment().isHidden());
|
||||
assertTrue(getPreSearchFragment().isVisible());
|
||||
assertTrue(getSearchFragment().isVisible());
|
||||
}
|
||||
|
||||
private void assertPostSearchVisible() {
|
||||
assertTrue(getPostSearchFragment().isVisible());
|
||||
assertTrue(getPreSearchFragment().isHidden());
|
||||
assertTrue(getSearchFragment().isVisible());
|
||||
}
|
||||
}
|
|
@ -15,9 +15,10 @@ package org.mozilla.search;
|
|||
* https://github.com/ericedens/FirefoxSearch/issues/3
|
||||
*/
|
||||
public class Constants {
|
||||
public static final String AUTO_COMPLETE_FRAGMENT = "org.mozilla.search.AUTO_COMPLETE_FRAGMENT";
|
||||
public static final String CARD_STREAM_FRAGMENT = "org.mozilla.search.CARD_STREAM_FRAGMENT";
|
||||
public static final String GECKO_VIEW_FRAGMENT = "org.mozilla.search.GECKO_VIEW_FRAGMENT";
|
||||
|
||||
public static final String POSTSEARCH_FRAGMENT = "org.mozilla.search.POSTSEARCH_FRAGMENT";
|
||||
public static final String PRESEARCH_FRAGMENT = "org.mozilla.search.PRESEARCH_FRAGMENT";
|
||||
public static final String SEARCH_FRAGMENT = "org.mozilla.search.SEARCH_FRAGMENT";
|
||||
|
||||
public static final String AUTOCOMPLETE_ROW_LIMIT = "5";
|
||||
}
|
||||
|
|
|
@ -7,13 +7,9 @@ package org.mozilla.search;
|
|||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.view.View;
|
||||
|
||||
import org.mozilla.search.autocomplete.AcceptsSearchQuery;
|
||||
import org.mozilla.search.autocomplete.AutoCompleteFragment;
|
||||
import org.mozilla.search.stream.CardStreamFragment;
|
||||
|
||||
|
||||
/**
|
||||
* The main entrance for the Android search intent.
|
||||
|
@ -23,81 +19,52 @@ import org.mozilla.search.stream.CardStreamFragment;
|
|||
* now, the only message passing occurs when a user wants to submit a search query. That
|
||||
* passes through the onSearch method here.
|
||||
*/
|
||||
public class MainActivity extends FragmentActivity implements AcceptsSearchQuery,
|
||||
FragmentManager.OnBackStackChangedListener {
|
||||
public class MainActivity extends FragmentActivity implements AcceptsSearchQuery {
|
||||
|
||||
private DetailActivity detailActivity;
|
||||
enum State {
|
||||
START,
|
||||
PRESEARCH,
|
||||
POSTSEARCH
|
||||
}
|
||||
|
||||
private State state = State.START;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle stateBundle) {
|
||||
super.onCreate(stateBundle);
|
||||
|
||||
// Sets the content view for the Activity
|
||||
setContentView(R.layout.search_activity_main);
|
||||
|
||||
// Gets an instance of the support library FragmentManager
|
||||
FragmentManager localFragmentManager = getSupportFragmentManager();
|
||||
|
||||
// If the incoming state of the Activity is null, sets the initial view to be thumbnails
|
||||
if (null == stateBundle) {
|
||||
|
||||
// Starts a Fragment transaction to track the stack
|
||||
FragmentTransaction localFragmentTransaction = localFragmentManager.beginTransaction();
|
||||
|
||||
localFragmentTransaction.add(R.id.header_fragments, new AutoCompleteFragment(),
|
||||
Constants.AUTO_COMPLETE_FRAGMENT);
|
||||
|
||||
localFragmentTransaction.add(R.id.presearch_fragments, new CardStreamFragment(),
|
||||
Constants.CARD_STREAM_FRAGMENT);
|
||||
|
||||
// Commits this transaction to display the Fragment
|
||||
localFragmentTransaction.commit();
|
||||
|
||||
// The incoming state of the Activity isn't null.
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
|
||||
|
||||
if (null == detailActivity) {
|
||||
detailActivity = new DetailActivity();
|
||||
}
|
||||
|
||||
if (null == getSupportFragmentManager().findFragmentByTag(Constants.GECKO_VIEW_FRAGMENT)) {
|
||||
FragmentTransaction txn = getSupportFragmentManager().beginTransaction();
|
||||
txn.add(R.id.gecko_fragments, detailActivity, Constants.GECKO_VIEW_FRAGMENT);
|
||||
txn.hide(detailActivity);
|
||||
|
||||
txn.commit();
|
||||
}
|
||||
startPresearch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSearch(String s) {
|
||||
FragmentManager localFragmentManager = getSupportFragmentManager();
|
||||
FragmentTransaction localFragmentTransaction = localFragmentManager.beginTransaction();
|
||||
|
||||
localFragmentTransaction
|
||||
.hide(localFragmentManager.findFragmentByTag(Constants.CARD_STREAM_FRAGMENT))
|
||||
.addToBackStack(null);
|
||||
|
||||
localFragmentTransaction
|
||||
.show(localFragmentManager.findFragmentByTag(Constants.GECKO_VIEW_FRAGMENT))
|
||||
.addToBackStack(null);
|
||||
|
||||
localFragmentTransaction.commit();
|
||||
|
||||
|
||||
((DetailActivity) getSupportFragmentManager()
|
||||
.findFragmentByTag(Constants.GECKO_VIEW_FRAGMENT))
|
||||
startPostsearch();
|
||||
((PostSearchFragment) getSupportFragmentManager().findFragmentById(R.id.gecko))
|
||||
.setUrl("https://search.yahoo.com/search?p=" + Uri.encode(s));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackStackChanged() {
|
||||
private void startPresearch() {
|
||||
if (state != State.PRESEARCH) {
|
||||
state = State.PRESEARCH;
|
||||
findViewById(R.id.gecko).setVisibility(View.INVISIBLE);
|
||||
findViewById(R.id.presearch).setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
private void startPostsearch() {
|
||||
if (state != State.POSTSEARCH) {
|
||||
state = State.POSTSEARCH;
|
||||
findViewById(R.id.presearch).setVisibility(View.INVISIBLE);
|
||||
findViewById(R.id.gecko).setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (state == State.POSTSEARCH) {
|
||||
startPresearch();
|
||||
} else {
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,15 +18,15 @@ import org.mozilla.gecko.GeckoViewChrome;
|
|||
import org.mozilla.gecko.GeckoViewContent;
|
||||
import org.mozilla.gecko.PrefsHelper;
|
||||
|
||||
public class DetailActivity extends Fragment {
|
||||
public class PostSearchFragment extends Fragment {
|
||||
|
||||
private static final String LOGTAG = "DetailActivity";
|
||||
private static final String LOGTAG = "PostSearchFragment";
|
||||
private GeckoView geckoView;
|
||||
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
Bundle savedInstanceState) {
|
||||
View mainView = inflater.inflate(R.layout.search_activity_detail, container, false);
|
||||
|
||||
|
|
@ -2,14 +2,14 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.search.stream;
|
||||
package org.mozilla.search;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.ListFragment;
|
||||
import android.view.View;
|
||||
import android.widget.ArrayAdapter;
|
||||
|
||||
import org.mozilla.search.R;
|
||||
import org.mozilla.search.stream.PreloadAgent;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -17,7 +17,7 @@ import org.mozilla.search.R;
|
|||
* we only use this during pre-search, but we could also use it
|
||||
* during post-search at some point.
|
||||
*/
|
||||
public class CardStreamFragment extends ListFragment {
|
||||
public class PreSearchFragment extends ListFragment {
|
||||
|
||||
private ArrayAdapter<PreloadAgent.TmpItem> adapter;
|
||||
|
||||
|
@ -25,7 +25,7 @@ public class CardStreamFragment extends ListFragment {
|
|||
* Mandatory empty constructor for the fragment manager to instantiate the
|
||||
* fragment (e.g. upon screen orientation changes).
|
||||
*/
|
||||
public CardStreamFragment() {
|
||||
public PreSearchFragment() {
|
||||
}
|
||||
|
||||
@Override
|
|
@ -33,7 +33,7 @@ import org.mozilla.search.R;
|
|||
* TODO: Add clear button to search input
|
||||
* TODO: Add more search providers (other than the dictionary)
|
||||
*/
|
||||
public class AutoCompleteFragment extends Fragment implements AdapterView.OnItemClickListener,
|
||||
public class SearchFragment extends Fragment implements AdapterView.OnItemClickListener,
|
||||
TextView.OnEditorActionListener, AcceptsJumpTaps {
|
||||
|
||||
private View mainView;
|
||||
|
@ -50,13 +50,13 @@ public class AutoCompleteFragment extends Fragment implements AdapterView.OnItem
|
|||
RUNNING // The user is in search mode.
|
||||
}
|
||||
|
||||
public AutoCompleteFragment() {
|
||||
public SearchFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
|
||||
mainView = inflater.inflate(R.layout.search_auto_complete, container, false);
|
|
@ -12,10 +12,10 @@ import java.util.Map;
|
|||
/**
|
||||
* A temporary agent for loading cards into the pre-load card stream.
|
||||
* <p/>
|
||||
* When we have more agents, we'll want to put an agent manager between the CardStreamFragment
|
||||
* When we have more agents, we'll want to put an agent manager between the PreSearchFragment
|
||||
* and the set of all agents. See autocomplete.AutoCompleteFragmentManager.
|
||||
*/
|
||||
class PreloadAgent {
|
||||
public class PreloadAgent {
|
||||
|
||||
public static final List<TmpItem> ITEMS = new ArrayList<TmpItem>();
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
tools:context="org.mozilla.search.DetailActivity">
|
||||
tools:context="org.mozilla.search.PostSearchFragment">
|
||||
|
||||
<org.mozilla.gecko.GeckoView
|
||||
android:id="@+id/gecko_view"
|
||||
|
|
|
@ -10,26 +10,25 @@
|
|||
android:orientation="vertical"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/gecko_fragments"
|
||||
<fragment
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="45dp"
|
||||
android:name="org.mozilla.search.PostSearchFragment"
|
||||
android:id="@+id/gecko"
|
||||
/>
|
||||
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/presearch_fragments"
|
||||
<fragment
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:name="org.mozilla.search.PreSearchFragment"
|
||||
android:id="@+id/presearch"
|
||||
/>
|
||||
|
||||
<FrameLayout
|
||||
|
||||
<fragment
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:name="org.mozilla.search.autocomplete.SearchFragment"
|
||||
android:id="@+id/header_fragments"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
/>
|
||||
|
||||
</merge>
|
||||
</merge>
|
||||
|
|
Загрузка…
Ссылка в новой задаче