public class SearchFragment extends Fragment
SearchFragment.SearchResultProvider
interface to handle the search and return
an ObjectAdapter
containing the results. The results are rendered
into a RowsFragment
, in the same way that they are in a BrowseFragment
.
If you do not supply a callback via
setSpeechRecognitionCallback(SpeechRecognitionCallback)
, an internal speech
recognizer will be used for which your application will need to declare
android.permission.RECORD_AUDIO in AndroidManifest file. If app's target version is >= 23 and
the device version is >= 23, a permission dialog will show first time using speech recognition.
0 will be used as requestCode in requestPermissions() call.
Speech recognition is automatically started when fragment is created, but
not when fragment is restored from an instance state. Activity may manually
call startRecognition()
, typically in onNewIntent().
Modifier and Type | Class and Description |
---|---|
static interface |
SearchFragment.SearchResultProvider
Search API to be provided by the application.
|
Fragment.InstantiationException, Fragment.SavedState
TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_UI_HIDDEN
Constructor and Description |
---|
SearchFragment() |
Modifier and Type | Method and Description |
---|---|
static Bundle |
createArgs(Bundle args,
String query) |
static Bundle |
createArgs(Bundle args,
String query,
String title) |
void |
displayCompletions(CompletionInfo[] completions)
Displays the completions shown by the IME.
|
void |
displayCompletions(List<String> completions)
Displays the completions shown by the IME.
|
Drawable |
getBadgeDrawable()
Returns the badge drawable in the search bar.
|
Intent |
getRecognizerIntent()
Returns an intent that can be used to request speech recognition.
|
String |
getTitle()
Returns the title set in the search bar.
|
static SearchFragment |
newInstance(String query)
Creates a search fragment with a given search query.
|
void |
onCreate(Bundle savedInstanceState)
Called to do initial creation of a fragment.
|
View |
onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
Called to have the fragment instantiate its user interface view.
|
void |
onDestroy()
Called when the fragment is no longer in use.
|
void |
onPause()
Called when the Fragment is no longer resumed.
|
void |
onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults)
Callback for the result from requesting permissions.
|
void |
onResume()
Called when the fragment is visible to the user and actively running.
|
void |
onStart()
Called when the Fragment is visible to the user.
|
void |
setBadgeDrawable(Drawable drawable)
Sets the badge drawable that will be shown inside the search bar next to
the title.
|
void |
setOnItemViewClickedListener(OnItemViewClickedListener listener)
Sets an item clicked listener for the results.
|
void |
setOnItemViewSelectedListener(OnItemViewSelectedListener listener)
Sets an item selection listener for the results.
|
void |
setSearchQuery(Intent intent,
boolean submit)
Sets the text of the search query based on the
RecognizerIntent.EXTRA_RESULTS in
the given intent, and optionally submit the query. |
void |
setSearchQuery(String query,
boolean submit)
Sets the text of the search query and optionally submits the query.
|
void |
setSearchResultProvider(SearchFragment.SearchResultProvider searchResultProvider)
Sets the search provider that is responsible for returning results for the
search query.
|
void |
setSpeechRecognitionCallback(SpeechRecognitionCallback callback)
Sets this callback to have the fragment pass speech recognition requests
to the activity rather than using an internal recognizer.
|
void |
setTitle(String title)
Sets the title string to be be shown in an empty search bar.
|
void |
startRecognition()
Starts speech recognition.
|
dump, equals, getActivity, getAllowEnterTransitionOverlap, getAllowReturnTransitionOverlap, getArguments, getChildFragmentManager, getContext, getEnterTransition, getExitTransition, getFragmentManager, getHost, getId, getLayoutInflater, getLoaderManager, getParentFragment, getReenterTransition, getResources, getRetainInstance, getReturnTransition, getSharedElementEnterTransition, getSharedElementReturnTransition, getString, getString, getTag, getTargetFragment, getTargetRequestCode, getText, getUserVisibleHint, getView, hashCode, instantiate, instantiate, isAdded, isDetached, isHidden, isInLayout, isRemoving, isResumed, isVisible, onActivityCreated, onActivityResult, onAttach, onAttach, onAttachFragment, onConfigurationChanged, onContextItemSelected, onCreateAnimator, onCreateContextMenu, onCreateOptionsMenu, onDestroyOptionsMenu, onDestroyView, onDetach, onHiddenChanged, onInflate, onInflate, onInflate, onLowMemory, onMultiWindowModeChanged, onOptionsItemSelected, onOptionsMenuClosed, onPictureInPictureModeChanged, onPrepareOptionsMenu, onSaveInstanceState, onStop, onTrimMemory, onViewCreated, onViewStateRestored, registerForContextMenu, requestPermissions, setAllowEnterTransitionOverlap, setAllowReturnTransitionOverlap, setArguments, setEnterSharedElementCallback, setEnterSharedElementTransitionCallback, setEnterTransition, setExitSharedElementCallback, setExitSharedElementTransitionCallback, setExitTransition, setHasOptionsMenu, setInitialSavedState, setMenuVisibility, setReenterTransition, setRetainInstance, setReturnTransition, setSharedElementEnterTransition, setSharedElementReturnTransition, setTargetFragment, setUserVisibleHint, shouldShowRequestPermissionRationale, startActivity, startActivity, startActivityForResult, startActivityForResult, startIntentSenderForResult, toString, unregisterForContextMenu
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
Fragment
Fragment.requestPermissions(String[], int)
.
Note: It is possible that the permissions request interaction with the user is interrupted. In this case you will receive empty permissions and results arrays which should be treated as a cancellation.
onRequestPermissionsResult
in class Fragment
requestCode
- The request code passed in Fragment.requestPermissions(String[], int)
.permissions
- The requested permissions. Never null.grantResults
- The grant results for the corresponding permissions
which is either PackageManager.PERMISSION_GRANTED
or PackageManager.PERMISSION_DENIED
. Never null.Fragment.requestPermissions(String[], int)
public static Bundle createArgs(Bundle args, String query)
args
- Bundle to use for the arguments, if null a new Bundle will be created.public static SearchFragment newInstance(String query)
You should only use this if you need to start the search fragment with a pre-filled query.
query
- The search query to begin with.public void onCreate(Bundle savedInstanceState)
Fragment
Fragment.onAttach(Activity)
and before
Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle)
, but is not called if the fragment
instance is retained across Activity re-creation (see Fragment.setRetainInstance(boolean)
).
Note that this can be called while the fragment's activity is
still in the process of being created. As such, you can not rely
on things like the activity's content view hierarchy being initialized
at this point. If you want to do work once the activity itself is
created, see Fragment.onActivityCreated(Bundle)
.
If your app's targetSdkVersion
is Build.VERSION_CODES.M
or lower, child fragments being restored from the savedInstanceState are restored after
onCreate
returns. When targeting Build.VERSION_CODES.N
or
above and running on an N or newer platform version
they are restored by Fragment.onCreate
.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
Fragment
Fragment.onCreate(Bundle)
and Fragment.onActivityCreated(Bundle)
.
If you return a View from here, you will later be called in
Fragment.onDestroyView()
when the view is being released.
onCreateView
in class Fragment
inflater
- The LayoutInflater object that can be used to inflate
any views in the fragment,container
- If non-null, this is the parent view that the fragment's
UI should be attached to. The fragment should not add the view itself,
but this can be used to generate the LayoutParams of the view.savedInstanceState
- If non-null, this fragment is being re-constructed
from a previous saved state as given here.public void onStart()
Fragment
Activity.onStart
of the containing
Activity's lifecycle.public void onResume()
Fragment
Activity.onResume
of the containing
Activity's lifecycle.public void onPause()
Fragment
Activity.onPause
of the containing
Activity's lifecycle.public void onDestroy()
Fragment
Fragment.onStop()
and before Fragment.onDetach()
.public void startRecognition()
public void setSearchResultProvider(SearchFragment.SearchResultProvider searchResultProvider)
public void setOnItemViewSelectedListener(OnItemViewSelectedListener listener)
listener
- The item selection listener to be invoked when an item in
the search results is selected.public void setOnItemViewClickedListener(OnItemViewClickedListener listener)
listener
- The item clicked listener to be invoked when an item in
the search results is clicked.public void setTitle(String title)
public String getTitle()
public void setBadgeDrawable(Drawable drawable)
public Drawable getBadgeDrawable()
public void displayCompletions(List<String> completions)
completions
- A list of completions to show in the IME. Setting to
null or empty will clear the list.public void displayCompletions(CompletionInfo[] completions)
completions
- A list of completions to show in the IME. Setting to
null or empty will clear the list.public void setSpeechRecognitionCallback(SpeechRecognitionCallback callback)
public void setSearchQuery(String query, boolean submit)
onQueryTextChange
or
onQueryTextSubmit
will be
called on the provider if it is set.query
- The search query to set.submit
- Whether to submit the query.public void setSearchQuery(Intent intent, boolean submit)
RecognizerIntent.EXTRA_RESULTS
in
the given intent, and optionally submit the query. If more than one result is present
in the results list, the first will be used.intent
- Intent received from a speech recognition service.submit
- Whether to submit the query.public Intent getRecognizerIntent()
RecognizerIntent.ACTION_RECOGNIZE_SPEECH
plus
extras:
RecognizerIntent.EXTRA_LANGUAGE_MODEL
set to
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
RecognizerIntent.EXTRA_PARTIAL_RESULTS
set to trueRecognizerIntent.EXTRA_PROMPT
set to the search bar hint textsetSearchQuery(Intent, boolean)
.